Github   Telegram
Loading...
Searching...
No Matches
Public Member Functions | List of all members
concurrent::Variable< Data, Mutex > Class Template Referencefinal

#include <userver/concurrent/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
 

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.

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 109 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 111 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 103 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 95 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 97 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 91 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 67 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 86 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 81 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 76 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 71 of file variable.hpp.


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