Mutexes and locking
(Created page with " ===Mutex=== A Mutex is an object that enforces [http://en.wikipedia.org/wiki/Mutual_exclusion mutual exclusion] in accesses to a resource from multiple [[http://en.wikipedia...") |
Revision as of 14:05, 19 November 2012
Mutex
A Mutex is an object that enforces mutual exclusion in accesses to a resource from multiple [[1]]. This is a critical component of any multi-threaded program like Yate.
Yate's implementation provides a class named Mutex. Each instance of this class provides one point of mutual exclusion.
Lock
A helper class named Lock is also available. Each such object holds a Mutex locked for the duration of its existence. The most common use is to create an automatic (stack allocated) Lock object that will hold the mutex locked until the code exits the block in which the Lock object exists. The compiler generated destructor then releases the mutex no matter what way the instruction block is left - it could be a break, continue, return or even goto.
Lock2
Another helper class named Lock2 allows to lock two mutexes in a similar way with Lock. Both or none of the Mutex objects are locked. An important note is that the order of locking does not depend on the order of arguments to Lock2's constructor, instead the mutexes are ordered by their address in memory. This guarantees that no deadlock occurs if two threads create a Lock2 object if they lock same mutexes but in different order.
See also: