C++ Portable Types Library (PTypes) Version 1.7

Top: Multithreading: mutex

#include <pasync.h>

void mutex::enter();
void mutex::leave();

void mutex::lock();      // alias for enter()
void mutex::unlock();    // alias for leave()

Mutex (mutual exclusion) is another synchronization object which helps to protect shared data structures from being concurrently accessed and modified.

Accessing and changing simple variables like int concurrently can be considered safe provided that the variable is aligned on a boundary "native" to the given CPU (32 bits on most systems). More often, however, applications use more complex shared data structures which can not be modified and accessed at the same time. Otherwise, the logical integrity of the structure might be corrupt from the "reader's" point of view when some other process sequentially modifies the fields of a shared structure.

To logically lock the part of code which modifies such complex structures the thread creates a mutex object and embraces the critical code with calls to enter() and leave(). Reading threads should also mark their transactions with enter() and leave(). When either a reader or a writer enters the critical section, any attempt to enter the same section concurrently causes the thread to "hang" until the first thread leaves the critical section.

If more than two threads are trying to lock the same critical section, mutex builds a queue for them and allows threads to enter the section only one by one. The order of entering the section is platform dependent.

To avoid infinite locks on a mutex object, applications usually put the critical section into try {} block and call leave() from within catch {} in case an exception is raised during the transaction.

More often applications use a smarter mutual exclusion object called read/write lock -- rwlock. In some situations it is also possible to define the order of modification of logically dependent data such that synchronization is not necessary at all. This technique should be used with great care.

PTypes' mutex object encapsulates either Windows CRITICAL_SECTION structure or POSIX mutex object and implements the minimal set of features common to both platforms. Mutex can not be shared between processes. Note: mutex may not be reentrant on POSIX systems, i.e. a recursive lock from one thread may cause deadlock.

mutex::mutex() creates a mutex object.

void mutex::enter() marks the start of an indivisible transaction.

void mutex::leave() marks the end of an indivisible transaction.

void mutex::lock() is an alias for enter().

void mutex::unlock() is an alias for leave().

See also: thread, rwlock, trigger, semaphore, Examples

PTypes home