Mutex Class Reference

#include <Pt/System/Mutex.h>

Mutual exclusion device. More...

Inherits NonCopyable.

Public Member Functions

 Mutex ()
 Default constructor.
 ~Mutex ()
 Destructor.
void lock ()
 Lock the mutex.
bool tryLock ()
 Tries to lock the mutex.
void unlock ()
 Unlocks the mutex.
bool unlockNoThrow ()
 Unlocks the mutex.

Detailed Description

Mutex serializes access to shared data. lock() waits if another thread holds it. The same thread must not lock it again. The mutex must be unlocked when it is destroyed.

MutexLock locks in the constructor and unlocks in the destructor, including during stack unwinding.

class Counter
{
public:
Counter()
: _value(0)
{ }
void increment()
{
Pt::System::MutexLock lock(_mutex);
++_value;
}
private:
int _value;
};
Scoped lock for a Mutex.
Definition Mutex.h:127
Mutual exclusion device.
Definition Mutex.h:73

Constructor & Destructor Documentation

◆ ~Mutex()

~Mutex ( )

The destructor destroys the mutex. The mutex must be in unlocked state when the destructor is called.

Member Function Documentation

◆ lock()

void lock ( )

If the mutex is currently locked by another thread, the calling thread suspends until no other thread holds a lock on it. If the mutex is already locked by the calling thread a deadlock occurs.

◆ tryLock()

bool tryLock ( )

If the mutex is currently locked by another thread, false is returned, otherwise the lock is acquired.

◆ unlockNoThrow()

bool unlockNoThrow ( )

This method does not throw an exception if unlocking the mutex fails but simply returns false.