-
Notifications
You must be signed in to change notification settings - Fork 0
/
UnboundedQueue.cpp
110 lines (102 loc) · 2.42 KB
/
UnboundedQueue.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
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
//
// Created by amiramyss on 6/14/22.
//
#pragma once
#include <queue>
#include <string>
#include <semaphore.h>
#include <memory>
#include "Mutex.h"
#include "MutexScope.h"
#include "Semaphore.h"
/**
* Thread safe unlimited queue.
* @tparam T
*/
template<class T>
class UnboundedQueue {
private:
//pthread_mutex_t _lock;
Mutex _lock;
//sem_t _full;
std::queue<T> _queue;
std::unique_ptr<Semaphore> _full;
public:
UnboundedQueue(const UnboundedQueue<T>&) = delete;
UnboundedQueue& operator=(const UnboundedQueue<T>&) = delete;
public:
/**
* Constructor.
*/
UnboundedQueue() : _lock(), _full(std::make_unique<Semaphore>(0)) {
//sem_init(&_full, 0, 0);
//pthread_mutex_unlock(&_lock);
//pthread_mutex_init(&_lock, nullptr);
}
/*
* Pop from queue.
* @return variable at the top.
*/
//std::string pop() {
T pop() {
T top_var;
//wait until there is at least 1 object in queue
//sem_wait(&_full);
_full->wait();
//std::cout<<"POST POP SEM WAIT: " <<std::endl;
// lock queue
{
//pthread_mutex_lock(&_lock);
MutexScope scope(_lock);
//do operations on queue
top_var = _queue.front();
_queue.pop();
//pthread_mutex_unlock(&_lock);
}
//unlock queue, return upper value
return top_var;
};
/**
* Push to queue.
* @param var
*/
void push(T str) {
// no limitation here. just lock and proceed
{
MutexScope scope(_lock);
//pthread_mutex_lock(&_lock);
//push
_queue.push(str);
//unlock
//pthread_mutex_unlock(&_lock);
}
//sem_post(&_full); //full++
_full->post();
//std::cout << "SEM code " << std::to_string(x) << std::endl;
}
/**
* See top of queue.
* @return Top variable.
*/
T top() {
T str;
{
MutexScope scope(_lock);
//pthread_mutex_lock(&_lock);
if (_queue.empty())
str = nullptr;
else
str = _queue.front();
//pthread_mutex_unlock(&_lock);
}
return str;
}
/**
* Destructor.
*/
~UnboundedQueue() {
//pthread_mutex_destroy(&_lock);
/*sem_close(&_full);
sem_destroy(&_full);*/
}
};