forked from RapidsAtHKUST/EngineRaceRapids
-
Notifications
You must be signed in to change notification settings - Fork 0
/
blocking_queue.h
54 lines (46 loc) · 1.46 KB
/
blocking_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
#pragma once
#include <mutex>
#include <condition_variable>
#include <deque>
#include <chrono>
using namespace std;
using namespace std::chrono;
template<typename T>
class blocking_queue {
private:
std::mutex d_mutex;
std::condition_variable d_condition;
std::deque<T> d_queue;
public:
void push(T const &value) {
{
std::unique_lock<std::mutex> lock(this->d_mutex);
d_queue.push_front(value);
}
this->d_condition.notify_one();
}
T pop(double &total_io_sleep_time_) {
auto range_clock_beg = high_resolution_clock::now();
std::unique_lock<std::mutex> lock(this->d_mutex);
this->d_condition.wait(lock, [=] { return !this->d_queue.empty(); });
T rc(std::move(this->d_queue.back()));
this->d_queue.pop_back();
auto range_clock_end = high_resolution_clock::now();
double sleep_time =
duration_cast<nanoseconds>(range_clock_end - range_clock_beg).count() /
static_cast<double>(1000000000);
total_io_sleep_time_ += sleep_time;
return rc;
}
T pop() {
std::unique_lock<std::mutex> lock(this->d_mutex);
this->d_condition.wait(lock, [=] { return !this->d_queue.empty(); });
T rc(std::move(this->d_queue.back()));
this->d_queue.pop_back();
return rc;
}
size_t size(){
std::unique_lock<std::mutex> lock(this->d_mutex);
return d_queue.size();
}
};