Mutexes and locking
Line 20: | Line 20: | ||
* [[Locking Issues]] | * [[Locking Issues]] | ||
* [[Important Mutexes]] | * [[Important Mutexes]] | ||
+ | |||
+ | [[Category:Programmers]] [[Category:Locking]] [[Category:Mutexes]] |
Latest revision as of 13:45, 4 November 2013
Yate's implementation provides classes for mutual exclusion and locking.
[edit] Mutex
A Mutex is an object that enforces mutual exclusion in accesses to a resource from multiple threads. 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.
[edit] 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.
[edit] 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: