BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
ModelView::threadsafe_stack< T > Class Template Reference

Thread-safe stack borrowed from Anthony Williams, C++ Concurrency in Action, Second edition. More...

Public Member Functions

 threadsafe_stack ()
 
 threadsafe_stack (const threadsafe_stack &other)
 
 ~threadsafe_stack ()
 
bool empty () const
 
threadsafe_stackoperator= (const threadsafe_stack &other)=delete
 
void push (T new_value)
 
void stop ()
 Terminates waiting in wait_and_pop methods. More...
 
std::shared_ptr< T > try_pop ()
 
bool try_pop (T &value)
 
void update_top (T new_value)
 Updates top value in a stack. More...
 
std::shared_ptr< T > wait_and_pop ()
 
void wait_and_pop (T &value)
 

Private Attributes

std::stack< T > data
 
std::condition_variable data_condition
 
std::atomic< bool > in_waiting_state {true}
 
std::mutex m
 

Detailed Description

template<typename T>
class ModelView::threadsafe_stack< T >

Thread-safe stack borrowed from Anthony Williams, C++ Concurrency in Action, Second edition.

Definition at line 41 of file threadsafestack.h.

Constructor & Destructor Documentation

◆ threadsafe_stack() [1/2]

template<typename T >
ModelView::threadsafe_stack< T >::threadsafe_stack ( )
inline

Definition at line 49 of file threadsafestack.h.

49 {}

◆ ~threadsafe_stack()

template<typename T >
ModelView::threadsafe_stack< T >::~threadsafe_stack ( )
inline

Definition at line 50 of file threadsafestack.h.

50 { stop(); }
void stop()
Terminates waiting in wait_and_pop methods.

References ModelView::threadsafe_stack< T >::stop().

Here is the call graph for this function:

◆ threadsafe_stack() [2/2]

template<typename T >
ModelView::threadsafe_stack< T >::threadsafe_stack ( const threadsafe_stack< T > &  other)
inline

Definition at line 51 of file threadsafestack.h.

52  {
53  std::lock_guard<std::mutex> lock(m);
54  data = other.data;
55  }

References ModelView::threadsafe_stack< T >::data, and ModelView::threadsafe_stack< T >::m.

Member Function Documentation

◆ empty()

template<typename T >
bool ModelView::threadsafe_stack< T >::empty ( ) const
inline

Definition at line 117 of file threadsafestack.h.

118  {
119  std::lock_guard<std::mutex> lock(m);
120  return data.empty();
121  }

References ModelView::threadsafe_stack< T >::data, and ModelView::threadsafe_stack< T >::m.

Referenced by TEST_F().

◆ operator=()

template<typename T >
threadsafe_stack& ModelView::threadsafe_stack< T >::operator= ( const threadsafe_stack< T > &  other)
delete

◆ push()

template<typename T >
void ModelView::threadsafe_stack< T >::push ( new_value)
inline

Definition at line 58 of file threadsafestack.h.

59  {
60  std::lock_guard<std::mutex> lock(m);
61  data.push(std::move(new_value));
62  data_condition.notify_one();
63  }
std::condition_variable data_condition

References ModelView::threadsafe_stack< T >::data, ModelView::threadsafe_stack< T >::data_condition, and ModelView::threadsafe_stack< T >::m.

Referenced by TEST_F().

◆ stop()

template<typename T >
void ModelView::threadsafe_stack< T >::stop ( )
inline

Terminates waiting in wait_and_pop methods.

Definition at line 125 of file threadsafestack.h.

126  {
127  std::lock_guard<std::mutex> lock(m);
128  in_waiting_state = false;
129  data_condition.notify_all();
130  }
std::atomic< bool > in_waiting_state

References ModelView::threadsafe_stack< T >::data_condition, ModelView::threadsafe_stack< T >::in_waiting_state, and ModelView::threadsafe_stack< T >::m.

Referenced by ModelView::threadsafe_stack< T >::~threadsafe_stack(), and TEST_F().

◆ try_pop() [1/2]

template<typename T >
std::shared_ptr<T> ModelView::threadsafe_stack< T >::try_pop ( )
inline

Definition at line 107 of file threadsafestack.h.

108  {
109  std::lock_guard<std::mutex> lock(m);
110  if (data.empty())
111  return std::shared_ptr<T>();
112  std::shared_ptr<T> res(std::make_shared<T>(std::move(data.top())));
113  data.pop();
114  return res;
115  }

References ModelView::threadsafe_stack< T >::data, and ModelView::threadsafe_stack< T >::m.

◆ try_pop() [2/2]

template<typename T >
bool ModelView::threadsafe_stack< T >::try_pop ( T &  value)
inline

Definition at line 97 of file threadsafestack.h.

98  {
99  std::lock_guard<std::mutex> lock(m);
100  if (data.empty())
101  return false;
102  value = std::move(data.top());
103  data.pop();
104  return true;
105  }

References ModelView::threadsafe_stack< T >::data, and ModelView::threadsafe_stack< T >::m.

Referenced by TEST_F().

◆ update_top()

template<typename T >
void ModelView::threadsafe_stack< T >::update_top ( new_value)
inline

Updates top value in a stack.

Definition at line 67 of file threadsafestack.h.

68  {
69  std::lock_guard<std::mutex> lock(m);
70  if (!data.empty())
71  data.pop();
72  data.push(std::move(new_value));
73  data_condition.notify_one();
74  }

References ModelView::threadsafe_stack< T >::data, ModelView::threadsafe_stack< T >::data_condition, and ModelView::threadsafe_stack< T >::m.

Referenced by TEST_F().

◆ wait_and_pop() [1/2]

template<typename T >
std::shared_ptr<T> ModelView::threadsafe_stack< T >::wait_and_pop ( )
inline

Definition at line 86 of file threadsafestack.h.

87  {
88  std::unique_lock<std::mutex> lock(m);
89  data_condition.wait(lock, [this] { return !data.empty() || !in_waiting_state; });
90  if (data.empty())
91  throw empty_stack();
92  std::shared_ptr<T> const res(std::make_shared<T>(std::move(data.top())));
93  data.pop();
94  return res;
95  }

References ModelView::threadsafe_stack< T >::data, ModelView::threadsafe_stack< T >::data_condition, ModelView::threadsafe_stack< T >::in_waiting_state, and ModelView::threadsafe_stack< T >::m.

◆ wait_and_pop() [2/2]

template<typename T >
void ModelView::threadsafe_stack< T >::wait_and_pop ( T &  value)
inline

Definition at line 76 of file threadsafestack.h.

77  {
78  std::unique_lock<std::mutex> lock(m);
79  data_condition.wait(lock, [this] { return !data.empty() || !in_waiting_state; });
80  if (data.empty())
81  throw empty_stack();
82  value = std::move(data.top());
83  data.pop();
84  }

References ModelView::threadsafe_stack< T >::data, ModelView::threadsafe_stack< T >::data_condition, ModelView::threadsafe_stack< T >::in_waiting_state, and ModelView::threadsafe_stack< T >::m.

Referenced by TEST_F().

Member Data Documentation

◆ data

◆ data_condition

◆ in_waiting_state

template<typename T >
std::atomic<bool> ModelView::threadsafe_stack< T >::in_waiting_state {true}
private

◆ m


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