communicationthread.h
Go to the documentation of this file.
1 
7 #ifndef COMMUNICATIONTHREAD_H
8 #define COMMUNICATIONTHREAD_H
9 
10 #include <condition_variable>
11 #include <mutex>
12 #include <thread>
13 
26 {
27 public:
28  bool active;
29  std::thread worker;
35  {
36  lock = std::unique_lock<std::mutex>(m);
37  notified = false;
38  active = false;
39  }
40 
46  CommunicationThread(CommunicationThread&& other) = delete;
47 
53  CommunicationThread(const CommunicationThread& other) = delete;
54 
61 
66  {
67  std::lock(m, other.m);
68  std::lock_guard<std::mutex> self_lock(m, std::adopt_lock);
69  std::lock_guard<std::mutex> other_lock(other.m, std::adopt_lock);
70  active = other.active;
71  notified = other.notified;
72  lock = std::unique_lock<std::mutex>(m);
73  return *this;
74  }
75 
79  virtual void startThread() = 0;
83  virtual void stopThread() = 0;
87  virtual void workerFunction() = 0;
91  inline void sleep()
92  {
93  notified = false;
94  while (!notified) // loop to avoid spurious wakeups
95  {
96  cond_var.wait(lock);
97  }
98  }
102  inline void wakeUp()
103  {
104  notified = true;
105  cond_var.notify_one();
106  }
107 
108 private:
109  mutable std::mutex m;
110  std::condition_variable cond_var;
111  std::unique_lock<std::mutex> lock;
112  bool notified;
113 };
114 
115 #endif // COMMUNICATIONTHREAD_H
virtual void workerFunction()=0
Generic threadded worker function of this object.
CommunicationThread & operator=(const CommunicationThread &other)
CommunicationThread default Copy assignment.
std::unique_lock< std::mutex > lock
void sleep()
Locks the current worker thread until wakeUp() is called.
virtual void stopThread()=0
Stop the threadded workload.
virtual void startThread()=0
Start the threadded workload.
The CommunicationThread class is the abstract base class of all thread objects used in the communicat...
void wakeUp()
Unlocks the current worker thread.
std::condition_variable cond_var
CommunicationThread & operator=(CommunicationThread &&other)=delete
CommunicationThread default Move assignment.
CommunicationThread()
CommunicationThread default constructor.


pses_ucbridge
Author(s): Sebastian Ehmes
autogenerated on Sat Oct 28 2017 19:16:13