ImFusion C++ SDK 4.5.0
ImFusion::Threading::Protected< T > Class Template Reference

#include <ImFusion/Core/Threading/Protected.h>

Thread-safe wrapper for a shared resource protected by a mutex. More...

Detailed Description

template<class T>
class ImFusion::Threading::Protected< T >

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.

Template Parameters
TThe type of the resource to protect. Can be any copyable or movable type.
Note
Protected<shared_ptr<T>> should not be used. Use SharedProtected<T> instead. Usage example:
// Create a synchronized vector
// Thread 1: Add elements safely
{
auto locked = syncVec.lock();
locked->push_back(1);
locked->push_back(2);
} // Lock released
// Thread 2: Read elements safely
{
auto locked = syncVec.lock();
for (int val : *locked) {
std::cout << val << std::endl;
}
} // Lock released
Locked lock()
Acquires exclusive access to the protected resource.
Definition Protected.h:207
Protected(T resource)
Constructs a Protected by copying or moving the resource.
Definition Protected.h:165
T endl(T... args)

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>
Protectedoperator= (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>
copy () const
 Returns a copy of the protected resource.

Constructor & Destructor Documentation

◆ Protected() [1/3]

template<class T>
ImFusion::Threading::Protected< T >::Protected ( T resource)
inline

Constructs a Protected by copying or moving the resource.

Parameters
resourceThe resource to protect. Will be moved or copied into managed storage.

◆ Protected() [2/3]

template<class T>
ImFusion::Threading::Protected< T >::Protected ( std::unique_ptr< T > resource)
inline

Constructs a Protected by taking ownership of a unique_ptr.

Parameters
resourceUnique pointer to the resource. Ownership is transferred.

◆ Protected() [3/3]

template<class T>
template<typename U = T, std::enable_if_t< std::is_copy_constructible_v< U >, int > = 0>
ImFusion::Threading::Protected< T >::Protected ( const Protected< T > & other)
inline

Copy constructor.

Copies the resource and creates a new mutex. Only available if T is copy constructible.

Member Function Documentation

◆ operator=()

template<class T>
template<typename U = T, std::enable_if_t< std::is_copy_assignable_v< U >, int > = 0>
Protected & ImFusion::Threading::Protected< T >::operator= ( const Protected< T > & other)
inline

Copy assignment operator.

Copies the resource value. Only available if T is copy assignable.

◆ lock()

template<class T>
Locked ImFusion::Threading::Protected< T >::lock ( )
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.

Note
This method will block if another thread currently holds the lock.
CAUTION: Calling two locking functions in the same scope can lead to deadlocks. Use lockAll(...) instead.
See also
lockAll

◆ copy()

template<class T>
template<typename U = T, std::enable_if_t< std::is_copy_constructible_v< U >, int > = 0>
T ImFusion::Threading::Protected< T >::copy ( ) const
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.

Note
This method will block if another thread currently holds the lock.

The documentation for this class was generated from the following file:
  • ImFusion/Core/Threading/Protected.h
Search Tab / S to search, Esc to close