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 59 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::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 62 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 110 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 112 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 104 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 96 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 98 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 92 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 64 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 68 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 87 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 82 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 77 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 72 of file variable.hpp.


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