-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathBlockingQueue.h
More file actions
75 lines (59 loc) · 1.17 KB
/
Copy pathBlockingQueue.h
File metadata and controls
75 lines (59 loc) · 1.17 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
#ifndef __YDX_BLOCKING_QUEUE_H__
#define __YDX_BLOCKING_QUEUE_H__
#include "ydx_mutex.h"
#include "ydx_condition.h"
#include <boost/noncopyable.hpp>
#include <deque>
#include <assert.h>
namespace ydx
{
template<typename T>
class BlockingQueue : boost::noncopyable
{
public:
BlockingQueue()
:cond_(MutexLock_),
{
}
void put(const T& x)
{
MutexLockGuard lock(MutexLock_);
queue_.push_back(x);
cond_.notify(); // wait morphing saves us
// http://www.domaigne.com/blog/computing/condvars-signal-with-mutex-locked-or-not/
}
void put(const T&& x)
{
MutexLockGuard lock(MutexLock_);
queue_.push_back(std::move(x));
cond_.notify(); // wait morphing saves us
// http://www.domaigne.com/blog/computing/condvars-signal-with-mutex-locked-or-not/
}
T take()
{
MutexLockGuard lock(MutexLock_);
while(queue_.empty())
{
cond_.wait();
}
T t(std::move(queue_.front()));
queue_.pop_front();
return t;
}
bool empty()
{
MutexLockGuard lock(MutexLock_);
return queue_.empty();
}
size_t size()
{
MutexLockGuard lock(MutexLock_);
return queue_.size();
}
private:
mutable MutexLock_;
Condition cond_;
std::deque<T> queue_;
};
};
#endif