CppCommon  1.0.4.1
C++ Common Library
Public Member Functions | List of all members
CppCommon::Mutex Class Reference

Mutex synchronization primitive. More...

#include <mutex.h>

Public Member Functions

 Mutex ()
 
 Mutex (const Mutex &)=delete
 
 Mutex (Mutex &&mutex)=delete
 
 ~Mutex ()
 
Mutexoperator= (const Mutex &)=delete
 
Mutexoperator= (Mutex &&mutex)=delete
 
bool TryLock ()
 Try to acquire mutex without block. More...
 
bool TryLockFor (const Timespan &timespan)
 Try to acquire mutex for the given timespan. More...
 
bool TryLockUntil (const UtcTimestamp &timestamp)
 Try to acquire mutex until the given timestamp. More...
 
void Lock ()
 Acquire mutex with block. More...
 
void Unlock ()
 Release mutex. More...
 

Detailed Description

Mutex synchronization primitive.

A mutex object facilitates protection against data races and allows thread-safe synchronization of data between threads. A thread obtains ownership of a mutex object by calling one of the lock functions and relinquishes ownership by calling the corresponding unlock function.

Thread-safe.

https://en.wikipedia.org/wiki/Mutual_exclusion

Examples
threads_mutex.cpp.

Definition at line 29 of file mutex.h.

Constructor & Destructor Documentation

◆ Mutex() [1/3]

CppCommon::Mutex::Mutex ( )

Definition at line 150 of file mutex.cpp.

◆ Mutex() [2/3]

CppCommon::Mutex::Mutex ( const Mutex )
delete

◆ Mutex() [3/3]

CppCommon::Mutex::Mutex ( Mutex &&  mutex)
delete

◆ ~Mutex()

CppCommon::Mutex::~Mutex ( )

Definition at line 161 of file mutex.cpp.

Member Function Documentation

◆ Lock()

void CppCommon::Mutex::Lock ( )

Acquire mutex with block.

Will block.

Definition at line 170 of file mutex.cpp.

◆ operator=() [1/2]

Mutex& CppCommon::Mutex::operator= ( const Mutex )
delete

◆ operator=() [2/2]

Mutex& CppCommon::Mutex::operator= ( Mutex &&  mutex)
delete

◆ TryLock()

bool CppCommon::Mutex::TryLock ( )

Try to acquire mutex without block.

Will not block.

Returns
'true' if the mutex was successfully acquired, 'false' if the mutex is busy

Definition at line 167 of file mutex.cpp.

◆ TryLockFor()

bool CppCommon::Mutex::TryLockFor ( const Timespan timespan)

Try to acquire mutex for the given timespan.

Will block for the given timespan in the worst case.

Parameters
timespan- Timespan to wait for the mutex
Returns
'true' if the mutex was successfully acquired, 'false' if the mutex is busy

Definition at line 168 of file mutex.cpp.

◆ TryLockUntil()

bool CppCommon::Mutex::TryLockUntil ( const UtcTimestamp timestamp)
inline

Try to acquire mutex until the given timestamp.

Will block until the given timestamp in the worst case.

Parameters
timestamp- Timestamp to stop wait for the mutex
Returns
'true' if the mutex was successfully acquired, 'false' if the mutex is busy

Definition at line 63 of file mutex.h.

◆ Unlock()

void CppCommon::Mutex::Unlock ( )

Release mutex.

Will not block.

Definition at line 171 of file mutex.cpp.


The documentation for this class was generated from the following files: