| THM1176InstrumentDriver 1.2 C++ API 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
11 namespace MTL {
12 namespace Synchronization {
13
14 //----------------------------------------------------------------------//
16 typedef std::mutex CMutex;
17
18 //----------------------------------------------------------------------//
20 typedef std::recursive_mutex CRecursiveMutex;
21
22 //----------------------------------------------------------------------//
24 class CSemaphore
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>
73 class CLockGuard
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
MTL::Synchronization::CLockGuard::~CLockGuard
virtual ~CLockGuard()
Destructor.
Definition Synchronization.h:87
MTL::Synchronization::CLockGuard::CLockGuard
CLockGuard(LockType &rLock)
Constructor.
Definition Synchronization.h:80
MTL::Synchronization::CSemaphore
Counting semaphore.
Definition Synchronization.h:25
MTL::Synchronization::CSemaphore::wait
void wait()
Wait for a semaphore.
Definition Synchronization.h:40
MTL::Synchronization::CSemaphore::wait
bool wait(unsigned long Timeout)
Wait for a semaphore for a given number of milliseconds.
Definition Synchronization.h:48
MTL::Synchronization::CSemaphore::notify
void notify()
Raise a semaphore.
Definition Synchronization.h:33
MTL::Synchronization::CSemaphore::try_wait
bool try_wait()
Check whether a semaphore has been raised.
Definition Synchronization.h:59
MTL::Synchronization
Definition Synchronization.h:12
MTL::Synchronization::CThread
std::thread CThread
Thread.
Definition Synchronization.h:95
MTL::Synchronization::CMutex
std::mutex CMutex
Mutex.
Definition Synchronization.h:16
MTL::Synchronization::CRecursiveMutex
std::recursive_mutex CRecursiveMutex
Recursive Mutex.
Definition Synchronization.h:20
MTL
Definition THM1176.h:73