Symbian
Symbian Developer Library

SYMBIAN OS V9.4

Feedback

[Index] [Previous] [Next]


Using Mutexes

Mutexes provide serialised access to shared resources. They are Kernel objects and, as such, are managed by the Kernel.

A mutex can be used by threads across any number of processes. If a resource is only shared between the threads within the same process, it can be more efficient to use a critical section.

Access to a mutex is through an RMutex handle.

Mutexes are similar to semaphores in that they have a TInt count value that is incremented by calling the Signal() member function of the mutex handle and decremented by calling the Wait() member function of the mutex handle. A mutex with a negative value implies that a thread must wait for access to the shared resource.

Unlike a semaphore, the count value of a mutex is always set to one when it is constructed.

The creator of a shared resource uses the CreateLocal() or CreateGlobal() member functions of RMutex to create a mutex and to open the handle to it. Any thread wishing to access the resource first calls Wait(); that thread then calls Signal() after completing its access.

The first thread to call Wait() returns immediately, and is free to continue execution, but any other threads that call Wait() will wait in a queue maintained by the mutex. Waiting threads are released on a first-in first-out basis when the thread currently accessing the resource calls Signal().

The nature of the shared resources should be such that any access completes in a relatively short time so that threads do not wait for extensive periods on the mutex.

In many cases, it may be better to use servers to serialize access to a shared resource (for example, the window server) rather than use a mutex.