Bites
A small library of miscellaneous C++ codes.
 All Classes Namespaces Functions Friends Pages
Mutexed.hpp
1 #ifndef BITES_MUTEXED_HPP_INCLUDED
2 #define BITES_MUTEXED_HPP_INCLUDED
3 
4 #include <mutex>
5 #include <bites.hpp>
6 
7 namespace bites {
8 
12 template<typename T>
13 class Mutexed {
14 
15 public:
19  Mutexed () {/* Empty. */}
20 
24  Mutexed (const T& value)
25  {
26  set(value);
27  }
28 
32  void set (const T& value)
33  {
34  std::lock_guard <std::mutex> locker (m_lock);
35  m_value = value;
36  }
37 
41  T get ()
42  {
43  std::lock_guard <std::mutex> locker (m_lock);
44  return m_value;
45  }
46 
47 private:
48  std::mutex m_lock;
49  T m_value;
50 };
51 
52 } // namespace bites.
53 
54 #endif // BITES_MUTEXED_HPP_INCLUDED
Definition: Mutexed.hpp:13
Mutexed(const T &value)
Definition: Mutexed.hpp:24
void set(const T &value)
Definition: Mutexed.hpp:32
Mutexed()
Definition: Mutexed.hpp:19