AUI Framework  develop
Cross-platform base for C++ UI apps
Loading...
Searching...
No Matches
AMutexWrapper< T, Lockable > Class Template Reference

Wraps the object with a Lockable, providing exclusive access layer, i.e., for thread-safety. More...

#include <AUI/Thread/AMutexWrapper.h>

Public Member Functions#

 AMutexWrapper (T value=T()) noexcept
 
void lock ()
 
AMutexWrapperoperator= (const T &rhs)
 
AMutexWrapperoperator= (T &&rhs) noexcept
 
template<typename U, std::enable_if_t< std::is_constructible_v< U, T > > * = 0>
 operator U () noexcept
 
void unlock ()
 
T & value () noexcept
 
T * operator-> () noexcept
 
- Public Member Functions inherited from aui::noncopyable
 noncopyable (const noncopyable &)=delete
 
noncopyableoperator= (const noncopyable &)=delete
 

Detailed Description#

template<typename T, typename Lockable = AMutex>
class AMutexWrapper< T, Lockable >
Template Parameters
TThe type of the value to be protected.
LockableA lockable type which manages locking behaviour, i.e, AMutex.

On debug builds, provides a runtime check for better diagnostics.

Aka boost::synchronized_value.

Implementing thread safety#

struct SharedResource {
  AString data;
};
sharedResource->data = "test"; // bad, will crash
...
// thread 1
std::unique_lock lock(sharedResource); //
sharedResource->data = "hello";        // ok
...
// thread 2
std::unique_lock lock(sharedResource); //
sharedResource->data = "world";        // ok, will be done before or after "hello", but not simultaneously
...
Wraps the object with a Lockable, providing exclusive access layer, i.e., for thread-safety.
Definition AMutexWrapper.h:49
Represents a Unicode character string.
Definition AString.h:38