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


2.3.1.4 Mutex Operations

To lock a mutex, use one of the following routines, depending on what you want to happen if the mutex is locked:

When a thread is finished accessing a piece of shared data, it unlocks the associated mutex by calling the pthread_ mutex_unlock routine. If other threads are waiting on the mutex, one is placed in the ready state. If more than one thread is waiting on the mutex, the scheduling policy (see Section 2.2.3.2) and the scheduling priority (see Section 2.2.3.3) determine which thread is readied, and the next running thread that requests it locks the mutex. The mutex is not automatically granted to the first waiter. If the unlocking thread attempts to relock the mutex before the first waiter gets a chance to run, the unlocking thread will succeed in relocking the mutex, and the first waiter may be forced to reblock.

You can delete a mutex and reclaim its storage by calling the pthread_mutex_destroy routine. Use these routines only after the mutex is no longer needed by any thread. A mutex cannot be deleted while it is locked.


Important Note!
DECthreads does not currently detect deadlock conditions involving more than one mutex, but may in the future. Never write code that depends upon DECthreads not reporting a particular error condition.