unilink  0.4.3
A simple C++ library for unified async communication
unilink::concurrency::ThreadSafeState< StateType > Class Template Reference

Thread-safe state management class. More...

#include <thread_safe_state.hpp>

Public Types

using State = StateType
 
using StateCallback = std::function< void(const State &)>
 
using StateCallbackHandle = size_t
 

Public Member Functions

 ThreadSafeState (const State &initial_state=State{})
 
 ThreadSafeState (const ThreadSafeState &)=delete
 
ThreadSafeStateoperator= (const ThreadSafeState &)=delete
 
 ThreadSafeState (ThreadSafeState &&)=delete
 
ThreadSafeStateoperator= (ThreadSafeState &&)=delete
 
State get_state () const
 
void set_state (const State &new_state)
 
void set_state (State &&new_state)
 
bool compare_and_set (const State &expected, const State &desired)
 
State exchange (const State &new_state)
 
StateCallbackHandle add_state_change_callback (StateCallback callback)
 
void remove_state_change_callback (StateCallbackHandle handle)
 
void clear_state_change_callbacks ()
 
void wait_for_state (const State &expected_state, std::chrono::milliseconds timeout=std::chrono::milliseconds(1000))
 
void wait_for_state_change (std::chrono::milliseconds timeout=std::chrono::milliseconds(1000))
 
bool is_state (const State &expected_state) const
 
void notify_state_change ()
 

Detailed Description

template<typename StateType>
class unilink::concurrency::ThreadSafeState< StateType >

Thread-safe state management class.

Provides thread-safe state management with read-write lock semantics. Multiple readers can access the state simultaneously, but only one writer can modify the state at a time.

Definition at line 41 of file thread_safe_state.hpp.

Member Typedef Documentation

◆ State

template<typename StateType >
using unilink::concurrency::ThreadSafeState< StateType >::State = StateType

Definition at line 43 of file thread_safe_state.hpp.

◆ StateCallback

template<typename StateType >
using unilink::concurrency::ThreadSafeState< StateType >::StateCallback = std::function<void(const State&)>

Definition at line 44 of file thread_safe_state.hpp.

◆ StateCallbackHandle

template<typename StateType >
using unilink::concurrency::ThreadSafeState< StateType >::StateCallbackHandle = size_t

Definition at line 45 of file thread_safe_state.hpp.

Constructor & Destructor Documentation

◆ ThreadSafeState() [1/3]

template<typename StateType >
unilink::concurrency::ThreadSafeState< StateType >::ThreadSafeState ( const State initial_state = State{})
explicit

Definition at line 171 of file thread_safe_state.hpp.

◆ ThreadSafeState() [2/3]

template<typename StateType >
unilink::concurrency::ThreadSafeState< StateType >::ThreadSafeState ( const ThreadSafeState< StateType > &  )
delete

◆ ThreadSafeState() [3/3]

template<typename StateType >
unilink::concurrency::ThreadSafeState< StateType >::ThreadSafeState ( ThreadSafeState< StateType > &&  )
delete

Member Function Documentation

◆ add_state_change_callback()

template<typename StateType >
ThreadSafeState< StateType >::StateCallbackHandle unilink::concurrency::ThreadSafeState< StateType >::add_state_change_callback ( StateCallback  callback)

Definition at line 230 of file thread_safe_state.hpp.

◆ clear_state_change_callbacks()

template<typename StateType >
void unilink::concurrency::ThreadSafeState< StateType >::clear_state_change_callbacks

Definition at line 247 of file thread_safe_state.hpp.

◆ compare_and_set()

template<typename StateType >
bool unilink::concurrency::ThreadSafeState< StateType >::compare_and_set ( const State expected,
const State desired 
)

Definition at line 202 of file thread_safe_state.hpp.

◆ exchange()

template<typename StateType >
StateType unilink::concurrency::ThreadSafeState< StateType >::exchange ( const State new_state)

Definition at line 216 of file thread_safe_state.hpp.

◆ get_state()

template<typename StateType >
StateType unilink::concurrency::ThreadSafeState< StateType >::get_state

Definition at line 174 of file thread_safe_state.hpp.

◆ is_state()

template<typename StateType >
bool unilink::concurrency::ThreadSafeState< StateType >::is_state ( const State expected_state) const

Definition at line 266 of file thread_safe_state.hpp.

◆ notify_state_change()

template<typename StateType >
void unilink::concurrency::ThreadSafeState< StateType >::notify_state_change

Definition at line 272 of file thread_safe_state.hpp.

◆ operator=() [1/2]

template<typename StateType >
ThreadSafeState& unilink::concurrency::ThreadSafeState< StateType >::operator= ( const ThreadSafeState< StateType > &  )
delete

◆ operator=() [2/2]

template<typename StateType >
ThreadSafeState& unilink::concurrency::ThreadSafeState< StateType >::operator= ( ThreadSafeState< StateType > &&  )
delete

◆ remove_state_change_callback()

template<typename StateType >
void unilink::concurrency::ThreadSafeState< StateType >::remove_state_change_callback ( StateCallbackHandle  handle)

Definition at line 239 of file thread_safe_state.hpp.

◆ set_state() [1/2]

template<typename StateType >
void unilink::concurrency::ThreadSafeState< StateType >::set_state ( const State new_state)

Definition at line 180 of file thread_safe_state.hpp.

◆ set_state() [2/2]

template<typename StateType >
void unilink::concurrency::ThreadSafeState< StateType >::set_state ( State &&  new_state)

Definition at line 191 of file thread_safe_state.hpp.

◆ wait_for_state()

template<typename StateType >
void unilink::concurrency::ThreadSafeState< StateType >::wait_for_state ( const State expected_state,
std::chrono::milliseconds  timeout = std::chrono::milliseconds(1000) 
)

Definition at line 253 of file thread_safe_state.hpp.

◆ wait_for_state_change()

template<typename StateType >
void unilink::concurrency::ThreadSafeState< StateType >::wait_for_state_change ( std::chrono::milliseconds  timeout = std::chrono::milliseconds(1000))

Definition at line 259 of file thread_safe_state.hpp.


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