-
Notifications
You must be signed in to change notification settings - Fork 0
/
myqueue.cpp
53 lines (45 loc) · 1.03 KB
/
myqueue.cpp
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
/*
* Parallel and Distributed Systems 2020/2021
*
* Double ended queue used for communications.
*
* */
#include <iostream>
#include <mutex>
#include <condition_variable>
#include <deque>
#include <vector>
#include <chrono>
#include <cstddef>
#include <math.h>
#include <string>
template <typename T>
class myqueue
{
private:
std::mutex d_mutex;
std::condition_variable d_condition;
std::deque<T> d_queue;
public:
myqueue(std::string s) { std::cout << "Created " << s << " queue " << std::endl; }
myqueue() {}
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() {
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;
}
};
//
// needed something to represent the EOS
// here we use null
//
#define EOS nullopt