[Return to Bookshelf] [Contents] [Previous Section] [Next Section] [Index] [Help]


2.3.1 Mutexes

A mutex (mutual exclusion object) is used by multiple threads to ensure the integrity of a shared resource that they access, most commonly shared data, by allowing only one thread to access it at a time. A mutex has two states, locked and unlocked. For each piece of shared data, all threads accessing that data must use the same mutex: each thread locks the mutex before it accesses the shared data and unlocks the mutex when it is finished accessing that data. If the mutex is locked by another thread, the thread requesting the lock either waits for the mutex to be unlocked or returns, depending on the lock routine called (see Figure 2-1).

Figure 2-1 Only One Thread Can Lock a Mutex

Each mutex must be initialized before use. DECthreads supports static initialization at compile time, using one of the macros provided in , as well as dynamic initialization at run time by calling pthread_mutex_init. This routine allows you to specify an attributes object, which allows you to specify the mutex type. The types of mutexes are described in the following sections.



[Return to Bookshelf] [Contents] [Previous Section] [Next Section] [Index] [Help]