A mutual exclusion (mutex) is used cooperatively between threads to ensure that only one of the cooperating threads is allowed to access the data or run certain application code at a time.
The word mutex is shorthand for a primitive object that provides MUTual EXclusion between threads. For the purposes of this introduction, you can think of mutexes as similar to critical sections and monitors.
The mutex is typically logically associated with the data it protects by the application. For example, PAYROLL DATA has a PAYROLL MUTEX associated with it. The application code always locks the PAYROLL MUTEX before accessing the PAYROLL DATA. The mutex prevents access to the data by a thread only if that thread uses the mutex before accessing the data.
Create, lock, unlock, and destroy are operations typically performed on a mutex. Any thread that successfully locks the mutex is the owner until it unlocks the mutex. Any thread that attempts to lock the mutex waits until the owner unlocks the mutex. When the owner unlocks the mutex, control is returned to one waiting thread with that thread becoming the owner of the mutex. There can be only one owner of a mutex.
Mutex wait operations can be recursive. Recursive mutexes allow the owner to lock the mutex repeatedly. The owner of the mutex remains the same until the number of unlock requests is the same as the number of successful lock requests. Mutex waits can time out after a user specified amount of time or can return immediately if they cannot acquire the lock. For more information, see your API set documentation about mutex primitives available for your application.