rippled
Loading...
Searching...
No Matches
semaphore.h
1
29#pragma once
30
31#include <condition_variable>
32#include <mutex>
33
34namespace xrpl {
35
36template <class Mutex, class CondVar>
38{
39private:
40 Mutex m_mutex;
41 CondVar m_cond;
43
44public:
46
50 explicit basic_semaphore(size_type count = 0) : m_count(count)
51 {
52 }
53
55 void
57 {
59 ++m_count;
60 m_cond.notify_one();
61 }
62
64 void
66 {
68 while (m_count == 0)
69 m_cond.wait(lock);
70 --m_count;
71 }
72
76 bool
78 {
80 if (m_count == 0)
81 return false;
82 --m_count;
83 return true;
84 }
85};
86
88
89} // namespace xrpl
std::size_t m_count
Definition semaphore.h:42
bool try_wait()
Perform a non-blocking wait.
Definition semaphore.h:77
basic_semaphore(size_type count=0)
Create the semaphore, with an optional initial count.
Definition semaphore.h:50
void notify()
Increment the count and unblock one waiting thread.
Definition semaphore.h:56
void wait()
Block until notify is called.
Definition semaphore.h:65
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5