Mutexes and locking

From Yate Documentation
Jump to: navigation, search

Yate's implementation provides classes for mutual exclusion and locking.


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.


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.


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:

Personal tools