-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLockQueue.h
More file actions
43 lines (41 loc) · 974 Bytes
/
Copy pathLockQueue.h
File metadata and controls
43 lines (41 loc) · 974 Bytes
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
#include<queue>
#include<mutex>
using namespace std;
/*
This is FIFO queue
*/
template <typename T>
class LockQueue {
public:
// insert a new element at the end of queue
void push(const T& val)
{
std::lock_guard<std::mutex> lock(mtx);
q.push(val);
}
// remove the oldest element
void pop()
{
std::lock_guard<std::mutex> lock(mtx);
q.pop();
}
// return the oldest element
T& front()
{
std::lock_guard<std::mutex> lock(mtx);
return q.front();
}
bool empty()
{
std::lock_guard<std::mutex> lock(mtx);
return q.empty();
}
size_t size()
{
std::lock_guard<std::mutex> lock(mtx);
return q.size();
}
private:
std::queue<T> q;
std::mutex mtx;
};