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

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

RAII lock guard that provides exclusive access to a synchronized resource. More...

Inheritance diagram for ImFusion::Threading::Protected< T >::Locked< isConst >:

Detailed Description

template<class T>
template<bool isConst = false>
class ImFusion::Threading::Protected< T >::Locked< isConst >

RAII lock guard that provides exclusive access to a synchronized resource.

This class combines a lock acquisition with access to the protected resource. The lock is acquired on construction and automatically released on destruction, following the RAII (Resource Acquisition Is Initialization) pattern.

Locked objects provide pointer-like semantics via operator* and operator-> to access the underlying resource while the lock is held.

Template Parameters
UThe type of the resource being protected

Usage example:

{
auto locked = syncVec.lock(); // Lock acquired here
locked->push_back(42); // Safe access while locked
} // Lock automatically released here
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
Note
This class is non-copyable and non-movable.

Public Types

using value_type = std::conditional_t<isConst, const T, T>
using return_reference = std::conditional_t<isConst, const value_type&, value_type&>
using return_ptr = std::conditional_t<isConst, const value_type*, value_type*>

Public Member Functions

 Locked (std::shared_ptr< value_type > value, std::shared_ptr< std::mutex > sharedMutex)
 Constructs a Locked instance and acquires the mutex.
 Locked (std::shared_ptr< value_type > resource, std::shared_ptr< std::mutex > sharedMutex, std::adopt_lock_t)
 Constructs a Locked instance adopting an already-held lock.
 Locked (std::tuple< std::shared_ptr< value_type >, std::shared_ptr< std::mutex >, std::adopt_lock_t > t)
 Constructs a Locked instance adopting an already-held lock.
 Locked (Locked &&other)=delete
return_reference operator* ()
 Provides mutable reference access to the locked resource.
return_ptr operator-> ()
 Provides mutable pointer access to the locked resource.
const value_type & operator* () const
 Provides const reference access to the locked resource.
const value_type * operator-> () const
 Provides const pointer access to the locked resource.

Additional Inherited Members

Protected Member Functions inherited from ImFusion::Utils::NotCopyable
 NotCopyable (NotCopyable &&) noexcept=default
NotCopyable & operator= (NotCopyable &&) noexcept=default
 NotCopyable (const NotCopyable &)=delete
NotCopyable & operator= (const NotCopyable &)=delete

Constructor & Destructor Documentation

◆ Locked() [1/3]

template<class T>
template<bool isConst = false>
ImFusion::Threading::Protected< T >::Locked< isConst >::Locked ( std::shared_ptr< value_type > value,
std::shared_ptr< std::mutex > sharedMutex )
inline

Constructs a Locked instance and acquires the mutex.

Parameters
valueShared pointer to the resource to protect. Must not be null.
sharedMutexShared pointer to the mutex protecting the resource. Must not be null.
Note
The lock is acquired immediately upon construction and held until the Locked object is destroyed.

◆ Locked() [2/3]

template<class T>
template<bool isConst = false>
ImFusion::Threading::Protected< T >::Locked< isConst >::Locked ( std::shared_ptr< value_type > resource,
std::shared_ptr< std::mutex > sharedMutex,
std::adopt_lock_t  )
inline

Constructs a Locked instance adopting an already-held lock.

Parameters
resourceShared pointer to the resource to protect. Must not be null.
sharedMutexShared pointer to the mutex protecting the resource. Must not be null. The mutex must already be locked by the calling thread.

◆ Locked() [3/3]

template<class T>
template<bool isConst = false>
ImFusion::Threading::Protected< T >::Locked< isConst >::Locked ( std::tuple< std::shared_ptr< value_type >, std::shared_ptr< std::mutex >, std::adopt_lock_t > t)
inline

Constructs a Locked instance adopting an already-held lock.

Only required for constructing tuples of Locked.

Parameters
tinput parameters packed as a tuple.

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