Async 1.8.0
|
Synchronize execution from a thread with the main Async thread. More...
#include <AsyncMutex.h>
Public Member Functions | |
Mutex (void) noexcept | |
Default constructor. | |
Mutex (const Mutex &)=delete | |
Copy constructor is deleted. | |
~Mutex (void) | |
Destructor. | |
Mutex & | operator= (const Mutex &)=delete |
Assignment operator is deleted. | |
void | lock (void) |
Lock the mutex. | |
void | unlock (void) |
Unlock the mutex. | |
Synchronize execution from a thread with the main Async thread.
This class is used to synchronize the execution of a thread with the main Async thread so that it is safe to access resources owned by the main thread that otherwise does not know about thread synchronization. When a thread holds the lock the main Async thread is guaranteed to be in a safe place. All other threads trying to get the same lock will be blocked until the thread holding the lock release it.
As always with locks, try to hold the lock the shortest time possible since it will block the main Async thread while the lock is held. That for example means that no timer handlers nor file descriptor watch handlers will be called
Note that locking this mutex is almost always expensive since the main Async thread own the lock if no other thread owns it. The main thread must then get to a safe place before the mutex can be locked by another thread. Only use this mutex if synchronization with the main Async thread is needed. If the purpose is to only synchronize between other threads, use a standard mutex. A standard mutex is also sufficient if synchronizing access to some data structure that you have full control over.
Note that all instances of Async::Mutexes are connected so that if one instance is locked, no other instance can be locked. That is, the other threads wanting to lock an Async::Mutex will be blocked while waiting for the lock to be unlocked.
The Async::Mutex may be used with standard C++ constructs like lock_guard, unique_lock, condition_variable_any, etc.
Definition at line 142 of file AsyncMutex.h.
|
noexcept |
Default constructor.
A mutex must be created on the main Async thread. Also it cannot be created before an Async::Application has been created. This means that an Async::Mutex cannot be declared as a global variable since those are initialized before the main function is called.
|
delete |
Copy constructor is deleted.
Async::Mutex::~Mutex | ( | void | ) |
Destructor.
A mutex must be destructed on the main Async thread.
The behavior is undefined if the mutex is owned by any thread or if any thread terminates while holding any ownership of the mutex.
void Async::Mutex::lock | ( | void | ) |
Lock the mutex.
Calling this function will lock the mutex if it's not already locked by another thread. If the lock is held by another thread this thread will block until the lock can be acquired. NOTE: If called from the main Async thread it's a noop since if we already are executing in the main thread we by definition own the lock.
void Async::Mutex::unlock | ( | void | ) |
Unlock the mutex.
Unlock a previously locked mutex. It is not allowed to call this function if the calling thread does not own the lock. NOTE: If called from the main Async thread it's a noop since if we already are executing in the main thread we by definition own the lock.