userver: concurrent::Variable< Data, Mutex > Class Template Reference
Loading...
Searching...
No Matches
concurrent::Variable< Data, Mutex > Class Template Referencefinal

#include <userver/concurrent/variable.hpp>

Detailed Description

template<typename Data, typename Mutex = engine::Mutex>
class concurrent::Variable< Data, Mutex >

Container for shared data protected with a mutex of any type (mutex, shared mutex, etc.).

Example usage:

constexpr auto kTestString = "Test string";
struct Data {
int x = 0;
std::string s{};
};
// If you do not specify the 2nd template parameter,
// then by default Variable is protected by engine::Mutex.
{
// We get a smart pointer to the data,
// the smart pointer stores std::lock_guard
auto data_ptr = data.Lock();
data_ptr->s = kTestString;
}
{
// We get a smart pointer to the data,
// the smart pointer stores std::shared_lock
auto data_ptr = data.SharedLock();
// we can read data, we cannot write
ASSERT_EQ(data_ptr->s, kTestString);
}
See also
Synchronization Primitives

Definition at line 58 of file variable.hpp.

Public Member Functions

template<typename... Arg>
 Variable (Arg &&... arg)
 
LockedPtr< std::unique_lock< Mutex >, Data > UniqueLock ()
 
LockedPtr< std::unique_lock< Mutex >, const Data > UniqueLock () const
 
std::optional< LockedPtr< std::unique_lock< Mutex >, const Data > > UniqueLock (std::try_to_lock_t) const
 
std::optional< LockedPtr< std::unique_lock< Mutex >, Data > > UniqueLock (std::try_to_lock_t)
 
std::optional< LockedPtr< std::unique_lock< Mutex >, const Data > > UniqueLock (std::chrono::milliseconds try_duration) const
 
std::optional< LockedPtr< std::unique_lock< Mutex >, Data > > UniqueLock (std::chrono::milliseconds try_duration)
 
LockedPtr< std::shared_lock< Mutex >, const Data > SharedLock () const
 
LockedPtr< std::shared_lock< Mutex >, Data > SharedMutableLockUnsafe ()
 
LockedPtr< std::lock_guard< Mutex >, Data > Lock ()
 
LockedPtr< std::lock_guard< Mutex >, const Data > Lock () const
 
Mutex & GetMutexUnsafe () const
 
Data & GetDataUnsafe ()
 
const Data & GetDataUnsafe () const
 

Constructor & Destructor Documentation

◆ Variable()

template<typename Data , typename Mutex = engine::Mutex>
template<typename... Arg>
concurrent::Variable< Data, Mutex >::Variable ( Arg &&... arg)
inline

Definition at line 61 of file variable.hpp.

Member Function Documentation

◆ GetDataUnsafe() [1/2]

template<typename Data , typename Mutex = engine::Mutex>
Data & concurrent::Variable< Data, Mutex >::GetDataUnsafe ( )
inline

Get raw data. Use with extreme caution, only for cases where it is impossible to access data with safe methods (e.g. std::scoped_lock with multiple mutexes). For simple use cases call Lock(), UniqueLock(), SharedLock() instead.

Definition at line 102 of file variable.hpp.

◆ GetDataUnsafe() [2/2]

template<typename Data , typename Mutex = engine::Mutex>
const Data & concurrent::Variable< Data, Mutex >::GetDataUnsafe ( ) const
inline

Definition at line 104 of file variable.hpp.

◆ GetMutexUnsafe()

template<typename Data , typename Mutex = engine::Mutex>
Mutex & concurrent::Variable< Data, Mutex >::GetMutexUnsafe ( ) const
inline

Get raw mutex. Use with caution. For simple use cases call Lock(), UniqueLock(), SharedLock() instead.

Definition at line 96 of file variable.hpp.

◆ Lock() [1/2]

template<typename Data , typename Mutex = engine::Mutex>
LockedPtr< std::lock_guard< Mutex >, Data > concurrent::Variable< Data, Mutex >::Lock ( )
inline

Definition at line 90 of file variable.hpp.

◆ Lock() [2/2]

template<typename Data , typename Mutex = engine::Mutex>
LockedPtr< std::lock_guard< Mutex >, const Data > concurrent::Variable< Data, Mutex >::Lock ( ) const
inline

Definition at line 92 of file variable.hpp.

◆ SharedLock()

template<typename Data , typename Mutex = engine::Mutex>
LockedPtr< std::shared_lock< Mutex >, const Data > concurrent::Variable< Data, Mutex >::SharedLock ( ) const
inline

Definition at line 84 of file variable.hpp.

◆ SharedMutableLockUnsafe()

template<typename Data , typename Mutex = engine::Mutex>
LockedPtr< std::shared_lock< Mutex >, Data > concurrent::Variable< Data, Mutex >::SharedMutableLockUnsafe ( )
inline

Useful for grabbing a reference to an object in a node-based container, e.g. std::unordered_map. Values must support concurrent modification.

Definition at line 88 of file variable.hpp.

◆ UniqueLock() [1/6]

template<typename Data , typename Mutex = engine::Mutex>
LockedPtr< std::unique_lock< Mutex >, Data > concurrent::Variable< Data, Mutex >::UniqueLock ( )
inline

Definition at line 63 of file variable.hpp.

◆ UniqueLock() [2/6]

template<typename Data , typename Mutex = engine::Mutex>
LockedPtr< std::unique_lock< Mutex >, const Data > concurrent::Variable< Data, Mutex >::UniqueLock ( ) const
inline

Definition at line 65 of file variable.hpp.

◆ UniqueLock() [3/6]

template<typename Data , typename Mutex = engine::Mutex>
std::optional< LockedPtr< std::unique_lock< Mutex >, Data > > concurrent::Variable< Data, Mutex >::UniqueLock ( std::chrono::milliseconds try_duration)
inline

Definition at line 80 of file variable.hpp.

◆ UniqueLock() [4/6]

template<typename Data , typename Mutex = engine::Mutex>
std::optional< LockedPtr< std::unique_lock< Mutex >, const Data > > concurrent::Variable< Data, Mutex >::UniqueLock ( std::chrono::milliseconds try_duration) const
inline

Definition at line 75 of file variable.hpp.

◆ UniqueLock() [5/6]

template<typename Data , typename Mutex = engine::Mutex>
std::optional< LockedPtr< std::unique_lock< Mutex >, Data > > concurrent::Variable< Data, Mutex >::UniqueLock ( std::try_to_lock_t )
inline

Definition at line 71 of file variable.hpp.

◆ UniqueLock() [6/6]

template<typename Data , typename Mutex = engine::Mutex>
std::optional< LockedPtr< std::unique_lock< Mutex >, const Data > > concurrent::Variable< Data, Mutex >::UniqueLock ( std::try_to_lock_t ) const
inline

Definition at line 67 of file variable.hpp.


The documentation for this class was generated from the following file: