THM1176InstrumentManager 1.1
Qt Object abstraction for Metrolab THM1176
Loading...
Searching...
No Matches
Synchronization.h
Go to the documentation of this file.
1
4
5#pragma once
6
7#include <mutex>
8#include <thread>
9#include <condition_variable>
10
11namespace MTL {
12 namespace Synchronization {
13
14 //----------------------------------------------------------------------//
16 typedef std::mutex CMutex;
17
18 //----------------------------------------------------------------------//
20 typedef std::recursive_mutex CRecursiveMutex;
21
22 //----------------------------------------------------------------------//
25 {
26 private:
27 std::mutex m_mutex;
28 std::condition_variable m_condition;
29 unsigned long m_count = 0; // Initialized as locked.
30
31 public:
33 void notify() {
34 std::unique_lock<decltype(m_mutex)> lock(m_mutex);
35 ++m_count;
36 m_condition.notify_one();
37 }
38
40 void wait() {
41 std::unique_lock<decltype(m_mutex)> lock(m_mutex);
42 while (!m_count) // Handle spurious wake-ups.
43 m_condition.wait(lock);
44 --m_count;
45 }
46
48 bool wait(unsigned long Timeout) {
49 std::unique_lock<decltype(m_mutex)> lock(m_mutex);
50 while (!m_count) { // Handle spurious wake-ups.
51 if (m_condition.wait_for(lock, std::chrono::milliseconds(Timeout)) == std::cv_status::timeout)
52 return false;
53 }
54 --m_count;
55 return true;
56 }
57
59 bool try_wait() {
60 std::unique_lock<decltype(m_mutex)> lock(m_mutex);
61 if (m_count) {
62 --m_count;
63 return true;
64 }
65 return false;
66 }
67 };
68
69 //----------------------------------------------------------------------//
72 template <typename LockType>
74 {
75 private:
76 LockType & m_rLock;
77 public:
80 CLockGuard(LockType & rLock)
81 : m_rLock(rLock)
82 {
83 m_rLock.lock();
84 }
85
87 virtual ~CLockGuard()
88 {
89 m_rLock.unlock();
90 }
91 };
92
93 //----------------------------------------------------------------------//
95 typedef std::thread CThread;
96
97 } // namespace Synchronization
98} // namespace MTL
CLockGuard(LockType &rLock)
Constructor.
void wait()
Wait for a semaphore.
bool wait(unsigned long Timeout)
Wait for a semaphore for a given number of milliseconds.
void notify()
Raise a semaphore.
bool try_wait()
Check whether a semaphore has been raised.
std::thread CThread
Thread.
std::mutex CMutex
Mutex.
std::recursive_mutex CRecursiveMutex
Recursive Mutex.