forked from Abc-Arbitrage/Disruptor-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlockingQueue.h
More file actions
65 lines (53 loc) · 1.43 KB
/
BlockingQueue.h
File metadata and controls
65 lines (53 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#pragma once
#include <mutex>
#include <deque>
namespace Disruptor
{
template
<
class T,
class TQueue = std::deque< T >
>
class BlockingQueue
{
public:
typedef std::mutex Mutex;
typedef std::lock_guard< Mutex > ScopedLock;
typedef std::unique_lock< Mutex > UniqueLock;
void push(const T& value)
{
{
ScopedLock lock(m_mutex);
m_queue.push_back(value);
}
m_conditionVariable.notify_one();
}
void push(T&& value)
{
{
ScopedLock lock(m_mutex);
m_queue.push_back(std::move(value));
}
m_conditionVariable.notify_one();
}
bool empty() const
{
ScopedLock lock(m_mutex);
return m_queue.empty();
}
template <class TDuration>
bool timedWaitAndPop(T& value, const TDuration& duration)
{
UniqueLock lock(m_mutex);
if (!m_conditionVariable.wait_for(lock, duration, [this]() -> bool { return !m_queue.empty(); }))
return false;
value = std::move(m_queue.front());
m_queue.pop_front();
return true;
}
private:
TQueue m_queue;
mutable Mutex m_mutex;
std::condition_variable m_conditionVariable;
};
} // namespace Disruptor