General Info | |
Interface: Mutex Files: lib/mutex.h Last change: 22/12/2001 Author: Luiz Henrique Shigunov |
Description
Structures |
Functions | |
|
|
This page describes Mutex user interface.
The ideia behind a Mutex interface is to make possible different implementations of this interface. One implementation can have a very fast mutex, but with no error checking; another can implement a recursive mutex, etc.
If we had a Sync interface with all types of synchronizers, each implementation would have to implement all synchronizers of the interface.
A mutex is a MUTual EXclusion synchronizer and is useful for protecting shared data structures from concurrent modifications, implementing critical sections and monitors.
A mutex has two possible states: unlocked (not owned by any thread), and locked (owned by one thread). A mutex can never be owned by two different threads simultaneously. A thread attempting to lock a mutex that is already locked by another thread is suspended until the owning thread unlocks the mutex first.
data are specific data and must be initialized with Init.
int Destroy(Mutex *m);
This function destroys mutex m, freeing its resources.
The mutex can't be in use.
typedef struct {
unsigned int data[4];
} Mutex;
0x00 - Destroy
Syntax
Description
Return value
int Init(Mutex *m, int prop);
This function initializes mutex m with properties prop.
prop isn't used yet.
int Lock(Mutex *m);
This function locks mutex m.
If the mutex is currently unlocked, it becomes locked and owned by the calling thread. If the mutex is already locked by another thread, this function suspends the calling thread until the mutex is unlocked.
If the mutex is already locked by the calling thread, the behavior depends on the implementation.
int TryLock(Mutex *m);
This functions tries to lock mutex m.
It behaves identically to Lock, but returns an error if the thread would block.
int Unlock(Mutex *m);
This function unlocks mutex m.