-
Notifications
You must be signed in to change notification settings - Fork 1
/
queue.h
94 lines (73 loc) · 1.29 KB
/
queue.h
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
88
89
90
91
92
93
94
/*
µ¥Ïò¶ÓÁÐ by ³ÂÎÄÇå
*/
#ifndef _CWQ_QUEUE_H
#define _CWQ_QUEUE_H
#include <deque>
#include <exception>
#include "mutex.h"
template<class T>
class Queue
{
protected:
std::deque<T> _d;
public:
typename std::deque<T>::size_type size() const {
return _d.size();
}
bool empty() const {
return _d.empty();
}
void push(const T& elem) {
_d.push_back(elem);
}
T pop() {
if (_d.empty()) throw std::exception();
T elem(_d.front());
_d.pop_front();
return elem;
}
T& front() {
if (_d.empty()) throw std::exception();
return _d.front();
}
};
template<class T>
class LockQueue
{
Mutex _m;
protected:
std::deque<T> _d;
public:
typename std::deque<T>::size_type size() {
LOCK lock(_m);
return _d.size();
}
bool empty() {
LOCK lock(_m);
return _d.empty();
}
void push(const T& elem) {
LOCK lock(_m);
_d.push_back(elem);
}
T pop() {
LOCK lock(_m);
if (_d.empty()) throw std::exception();
T elem(_d.front());
_d.pop_front();
return elem;
}
T& front() {
LOCK lock(_m);
if (_d.empty()) throw std::exception();
return _d.front();
}
inline void push_lock(const T& elem) {
return push(elem);
}
inline T pop_lock() {
return pop();
}
};
#endif