Mutual Exclusion


The built-in procedures that deal with mutexes for Mutual Exclusion (create, lock/unlock, and destroy the mutexes).

Preamble:
Mutual exclusion is the method of serializing access to shared resources. If programmer do not want a thread to be accessing a shared resource that is already in the process of being accessed by another thread, he can use a mutex.

Logically a mutex is a lock with only one key:
- If a thread wishes to access a shared resource, the thread must first gain the lock.
- Once it has the lock it may do what it wants with the shared resource without concerns of other threads accessing the shared resource because other threads will have to wait.
- Once the thread finishes using the shared resource, it unlocks the mutex, which allows other threads to access the shared resource.

A mutex is a lock that guarantees three things:
- Atomicity - Locking a mutex is an atomic operation, meaning that the operating system (or threads library) assures you that if you locked a mutex, no other thread succeeded in locking this mutex at the same time.
- Singularity - If a thread managed to lock a mutex, it is assured that no other thread will be able to lock the thread until the original thread releases the lock.
- Non-Busy Wait - If a thread attempts to lock a thread that was locked by a second thread, the first thread will be suspended (and will not consume any CPU resources) until the lock is freed by the second thread. At this time, the first thread will wake up and continue execution, having the mutex locked by it.

This is a protocol that serializes access to a shared resource.
Note that such a protocol must be enforced for resource a mutex is protecting across all threads that may touch the resource being protected (including the implicit main thread).

Mutex capability can be fully used even with a detached thread (only its handler is no longer accessible by its identifier).

Creating / Destructing a mutex
MutexCreate creates a mutex, returning a handle identifier which is to be referred to when destroying the mutex.
Mutexes created with MutexCreate should be destroyed when no longer needed or before the end of the program with MutexDestroy.

Create
- Syntax:
- Usage:
mutexid = MutexCreate
- Return value:
The any ptr handle (mutexid) to the mutex created, or the null pointer (0) on failure.

Destroy
- Syntax:
- Usage:
MutexDestroy( mutexid )
- Parameter:
mutexid
The any ptr handle of the mutex to be destroyed.

Description
The call to MutexCreate must be executed before creating any thread using it (and before its use in the thread that creates it).
The call to MutexDestroy must be executed after any threads using the mutex are no longer in use (and after its last use in the thread that destroys it).

Locking / Unlocking a mutex
MutexLock/MutexUnlock allow to lock/unlock a mutex by referring to its handle identifier get at its creation.

Lock
- Syntax:
- Usage:
MutexLock( mutexid )
- Parameter:
mutexid
The any ptr handle of the mutex to be locked.

Unlock
- Syntax:
- Usage:
MutexUnlock( mutexid )
- Parameter:
mutexid
The any ptr handle of the mutex to be unlocked.

Description
The code between the lock and unlock calls to the mutex, is referred to as a critical section.
Minimizing time spent in the critical section allows for greater concurrency because it potentially reduces the amount of time other threads must wait to gain the lock.
Therefore, it is important for a thread programmer to minimize critical sections if possible.

Pseudo-code section
By applying all proper above rules:
'  Principle of mutual exclusion between 2 threads
'  (connecting lines join the sender(s) and receiver(s) impacted by each action occurring during the sequence)
'
'          Thread                                         Other Thread
'      MUTEXLOCK(mutexID) <----------------.    .---> MUTEXLOCK(mutexID)
'      Do_something_with_exclusion    .--- | ---'     Do_something_with_exclusion
'      MUTEXUNLOCK(mutexID) ----------'    '--------- MUTEXUNLOCK(mutexID)

Example
The first example on the previous page (Threads) is modified so that each thread no longer displays a single character ("M" or "C") but now a sequence of three characters ("[M]" from the main thread, "(C)" for the child thread).
The tempo in each thread loop has been cut into three chunks to help interleave the display between threads.


See also
Back to Programmer's Guide
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki



sf.net phatcode