![]() |
ImFusion C++ SDK 4.5.0
|
#include <ImFusion/Core/Threading/Protected.h>
Thread-safe wrapper for a shared resource protected by a mutex. More...
Thread-safe wrapper for a shared resource protected by a mutex.
This class provides synchronized access to a resource of type T by wrapping it with an internal mutex. Access to the resource is granted through Locked<T> objects, which ensure that the mutex is held for the duration of access.
The Protected class manages both the resource and its protecting mutex, ensuring they have the same lifetime and are correctly paired.
| T | The type of the resource to protect. Can be any copyable or movable type. |
Classes | |
| class | Locked |
| RAII lock guard that provides exclusive access to a synchronized resource. More... | |
Public Types | |
| using | LockedConst = Locked<true> |
Public Member Functions | |
| Protected (T resource) | |
| Constructs a Protected by copying or moving the resource. | |
| Protected (std::unique_ptr< T > resource) | |
| Constructs a Protected by taking ownership of a unique_ptr. | |
| template<typename U = T, std::enable_if_t< std::is_copy_constructible_v< U >, int > = 0> | |
| Protected (const Protected &other) | |
| Copy constructor. | |
| template<typename U = T, std::enable_if_t< std::is_copy_assignable_v< U >, int > = 0> | |
| Protected & | operator= (const Protected &other) |
| Copy assignment operator. | |
| Locked | lock () |
| Acquires exclusive access to the protected resource. | |
| LockedConst | lock () const |
| template<typename U = T, std::enable_if_t< std::is_copy_constructible_v< U >, int > = 0> | |
| T | copy () const |
| Returns a copy of the protected resource. | |
|
inline |
Constructs a Protected by copying or moving the resource.
| resource | The resource to protect. Will be moved or copied into managed storage. |
|
inline |
Constructs a Protected by taking ownership of a unique_ptr.
| resource | Unique pointer to the resource. Ownership is transferred. |
|
inline |
Copy constructor.
Copies the resource and creates a new mutex. Only available if T is copy constructible.
|
inline |
Copy assignment operator.
Copies the resource value. Only available if T is copy assignable.
|
inlinenodiscard |
Acquires exclusive access to the protected resource.
Returns a Locked<T> object that holds the mutex lock and provides access to the underlying resource. The lock is held until the Locked object is destroyed.
|
inlinenodiscard |
Returns a copy of the protected resource.
Acquires the mutex, copies the resource, and returns the copy. Only available if T is copy constructible.