ASCO Aerial Autonomy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
atomic.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <boost/thread/mutex.hpp>
4 
9 template <class T> class Atomic {
10 public:
14  Atomic() = default;
15 
20  Atomic(const T &data) { this->set(data); }
21 
26  Atomic(const Atomic<T> &a) { this->set(a.get()); }
27 
32  void set(const T &data) {
33  boost::mutex::scoped_lock lock(mutex_);
34  data_ = data;
35  }
36 
41  T get() const {
42  boost::mutex::scoped_lock lock(mutex_);
43  T data_copy = data_;
44  return data_copy;
45  }
46 
51  void operator=(const Atomic<T> &a) { this->set(a.get()); }
52 
57  void operator=(const T &d) { this->set(d); }
58 
63  operator T() const { return this->get(); }
64 
65 private:
66  T data_;
67  mutable boost::mutex mutex_;
68 };
void set(const T &data)
Set the data.
Definition: atomic.h:32
Template class to create thread-safe variables with internal lock management.
Definition: atomic.h:9
void operator=(const T &d)
Assignment operator for data.
Definition: atomic.h:57
T get() const
Get the data.
Definition: atomic.h:41
void operator=(const Atomic< T > &a)
Assignment operator.
Definition: atomic.h:51
Atomic(const Atomic< T > &a)
Copy constructor.
Definition: atomic.h:26
Atomic()=default
Default constructor.
Atomic(const T &data)
Constructor that sets member data.
Definition: atomic.h:20