Skip to content

AMutexWrapper#

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

Header:#include <AUI/Thread/AMutexWrapper.h>
CMake:aui_link(my_target PUBLIC aui::core)

Detailed Description#

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

Aka boost::synchronized_value.

Implementing thread safety#

struct SharedResource {
  AString data;
};
AMutexWrapper<SharedResource, AMutex> sharedResource;
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
...