Mutex

This class represents a general purpose, recursive mutex.

version(!Tango)
class Mutex : Object.Monitor {
CRITICAL_SECTION m_hndl;
pthread_mutex_t m_hndl;
}

Constructors

this
this()

Initializes a mutex object.

this
this(Object o)

Initializes a mutex object and sets it as the monitor for o.

Destructor

A destructor is present on this object, but not explicitly documented in the source.

Members

Functions

lock
void lock()

If this lock is not already held by the caller, the lock is acquired, then the internal counter is incremented by one.

tryLock
bool tryLock()

If the lock is held by another caller, the method returns. Otherwise, the lock is acquired if it is not already held, and then the internal counter is incremented by one.

unlock
void unlock()

Decrements the internal lock count by one. If this brings the count to zero, the lock is released.

Examples

/////////////////////////////////////////////////////////////////////////////

1 auto mutex      = new Mutex;
2 int  numThreads = 10;
3 int  numTries   = 1000;
4 int  lockCount  = 0;
5 
6 void testFn()
7 {
8     for( int i = 0; i < numTries; ++i )
9     {
10         synchronized( mutex )
11         {
12             ++lockCount;
13         }
14     }
15 }
16 
17 auto group = new ThreadGroup;
18 
19 for( int i = 0; i < numThreads; ++i )
20     group.create( &testFn );
21 
22 group.joinAll();
23 assert( lockCount == numThreads * numTries );

Meta