-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConcurrentQueue.hpp
More file actions
87 lines (73 loc) · 1.75 KB
/
ConcurrentQueue.hpp
File metadata and controls
87 lines (73 loc) · 1.75 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#ifndef CONCURRENTQUEUE_HPP
#define CONCURRENTQUEUE_HPP
#include <boost/thread/mutex.hpp>
#include <boost/thread/condition_variable.hpp>
#include <deque>
#include <stdexcept>
namespace oneandone {
namespace nms {
class ConcurrentQueueInterruptException : public std::runtime_error
{
public:
explicit ConcurrentQueueInterruptException(const std::string &message)
: runtime_error(message) {}
};
template <typename T>
class ConcurrentQueue
{
public:
ConcurrentQueue()
{
Start();
}
public:
void Push(const T &value)
{
boost::mutex::scoped_lock lock(queue_mutex_);
if (!stop_) {
queue_.push_back(value);
ready_.notify_one();
}
}
void Pop(T &value)
{
boost::mutex::scoped_lock lock(queue_mutex_);
while (queue_.empty() && !stop_) {
ready_.wait(lock);
}
if (stop_) {
throw ConcurrentQueueInterruptException("Queue interrupted");
}
value = queue_.front();
queue_.pop_front();
if (queue_.empty())
empty_.notify_one();
}
void Start()
{
stop_ = false;
}
void Wait()
{
boost::mutex::scoped_lock lock(queue_mutex_);
while (!queue_.empty()) {
empty_.wait(lock);
}
}
void Stop()
{
boost::mutex::scoped_lock lock(queue_mutex_);
stop_ = true;
ready_.notify_all();
queue_.clear();
}
private:
std::deque<T> queue_;
boost::mutex queue_mutex_;
boost::condition_variable ready_;
boost::condition_variable empty_;
bool stop_;
};
} // namespace oneandone
} // namespace nms
#endif // CONCURRENTQUEUE_HPP