-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueue.h
More file actions
38 lines (34 loc) · 664 Bytes
/
Queue.h
File metadata and controls
38 lines (34 loc) · 664 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
/*
* Queue.h
*
* Created on: Feb 20, 2015
* Author: rmin
*/
#ifndef QUEUE_H_
#define QUEUE_H_
#include <mutex>
#include <queue>
#include <condition_variable>
template <typename T>
class Queue {
private:
std::mutex mutex_;
std::queue<T> queue_;
std::condition_variable cv;
public:
Queue() {}
~Queue() {}
T pop() {
std::unique_lock<std::mutex> lock(mutex_);
cv.wait(lock, [&]() {return !queue_.empty();});
T value = queue_.front();
queue_.pop();
return value;
}
void push(T value) {
std::unique_lock<std::mutex> lock(mutex_);
queue_.push(value);
cv.notify_one();
}
};
#endif /* QUEUE_H_ */