C++Instrument Catalog MFC 1.2
Metrolab C++InstrumentCatalog for MFC
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 try_wait() {
49 std::unique_lock<decltype(m_mutex)> lock(m_mutex);
50 if (m_count) {
51 --m_count;
52 return true;
53 }
54 return false;
55 }
56 };
57
58 //----------------------------------------------------------------------//
61 template <typename LockType>
63 {
64 private:
65 LockType & m_rLock;
66 public:
69 CLockGuard(LockType & rLock)
70 : m_rLock(rLock)
71 {
72 m_rLock.lock();
73 }
74
76 virtual ~CLockGuard()
77 {
78 m_rLock.unlock();
79 }
80 };
81
82 //----------------------------------------------------------------------//
84 typedef std::thread CThread;
85
86 } // namespace Synchronization
87} // namespace MTL
std::thread CThread
Thread.
std::mutex CMutex
Mutex.
std::recursive_mutex CRecursiveMutex
Recursive Mutex.
void wait()
Wait for a semaphore.
void notify()
Raise a semaphore.
bool try_wait()
Check whether a semaphore has been raised.
CLockGuard(LockType &rLock)
Constructor.