-
Notifications
You must be signed in to change notification settings - Fork 0
/
BoundedQueue.cpp
113 lines (104 loc) · 2.38 KB
/
BoundedQueue.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
111
112
113
//
// Created by amiramyss on 6/8/22.
//
#pragma once
#include <queue>
#include <string>
#include <semaphore.h>
#include <iostream>
#include <memory>
#include "Mutex.h"
#include "MutexScope.h"
#include "Semaphore.h"
/**
* Queue with fixed capacity. Thread safe.
* @tparam T
*/
template<class T>
class BoundedQueue {
private:
std::queue<T> _queue;
//pthread_mutex_t _lock;
Mutex _lock;
//sem_t _empty, _full;
unsigned int _capacity;
std::shared_ptr<Semaphore> _empty;
std::shared_ptr<Semaphore> _full;
public:
/**
* Constructor
* @param capacity Queue's capacity.
*/
BoundedQueue(unsigned int capacity = 0) :
_capacity(capacity), _queue(), _lock() ,
//_empty(new Semaphore(capacity)), _full(new Semaphore(0))
_empty(std::make_shared<Semaphore>(capacity)),
_full(std::make_shared<Semaphore>(0))
{
}
/**
* Push to queue.
* @param var
*/
void push(T var) {
//wait as long as queue full
//sem_wait(&_empty); //empty--;
_empty->wait();
{
MutexScope scope(_lock);
_queue.push(var);
//sem_post(&_full); //full++;
_full->post();
}
}
/**
* Pop from queue.
* @return variable at the top.
*/
T pop() {
T t;
//wait as long as there are no nodes in queue
//sem_wait(&_full); //full--
_full->wait();
{
//pthread_mutex_lock(&_lock);
MutexScope scope(_lock);
t = _queue.front();
_queue.pop();
//sem_post(&_empty); //empty++
_empty->post();
//removing 1 value
//pthread_mutex_unlock(&_lock);
}
return t;
}
/**
* See top of queue.
* @return Top variable.
*/
T top() {
T str;
//pthread_mutex_lock(&_lock);
{
MutexScope scope(_lock);
if (_queue.empty())
str = T();
else
str = _queue.front();
}
//pthread_mutex_unlock(&_lock);
return str;
}
/**
* Destructor.
*/
~BoundedQueue() {
//pthread_mutex_destroy(&_lock);
/*sem_close(&_empty);
sem_destroy(&_empty);
sem_close(&_full);
sem_destroy(&_full);*//*
delete _full;
delete _empty;*/
}
};