class sys.Lock
Overview
This class is used to ensure exclusive access to some shared resource. More…
import "sys_Lock.jnc" class Lock { // construction construct(); destruct(); // methods void lock(); void unlock(); };
Detailed Documentation
This class is used to ensure exclusive access to some shared resource.
Only one thread at a time can acquire the sys.Lock
object and hence,
get exclusive access to some resource this sys.Lock
object is being a
guardian of. To do that, a thread calls lock
method. Once it returns,
the sys.Lock
object is in locked state and this thread can exclusively
access the associated resource.
Any other thread trying to acquire it will wait until the original thread
releases the lock with unlock
method.
Code sample:
import "sys_Lock.jnc"
sys.Lock g_event;
int g_resource;
thread1() {
// ...
g_lock.lock(); // acquire the lock
g_resource++; // we can safely access the resource now
g_lock.unlock(); // let others get access, too
// continue...
}
thread2() {
// ...
g_lock.lock(); // acquire the lock
g_resource--; // we can safely access the resource now
g_lock.unlock(); // let others get access, too
// continue...
}
See also:
Methods
void lock()
Acquires the lock. If the lock is already acquired by another thread, the current thread enters the wait queue for the lock. When the lock is released by other thread(s), the current thread acquires it.
void unlock()
Releases the lock. If other threads are waiting for the lock at the moment, the first one in the wait queue acquires the lock.