-
Notifications
You must be signed in to change notification settings - Fork 0
/
BlockingQueue.h
60 lines (43 loc) · 1.01 KB
/
BlockingQueue.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
55
56
57
58
59
60
//
// Created by Armin on 13/04/2023.
//
#ifndef SUDOKU_BLOCKINGQUEUE_H
#define SUDOKU_BLOCKINGQUEUE_H
#include <queue>
#include "AutoCountableEvent.h"
template<class T>
class BlockingQueue {
AutoCountableEvent incomingData;
std::queue<T> queue;
std::mutex m;
public:
T getAndPopBlocking();
void push(T const & t);
void push(T && t);
void threadsWaiting(int waitingThreads);
};
template<class T>
void BlockingQueue<T>::threadsWaiting(int waitingThreads) {
incomingData.awaitAllWaiting(waitingThreads);
}
template<class T>
T BlockingQueue<T>::getAndPopBlocking() {
incomingData.waitOne();
std::lock_guard lk(m);
auto temp = queue.front();
queue.pop();
return temp;
}
template<class T>
void BlockingQueue<T>::push(T const & t) {
std::lock_guard lk(m);
queue.push(t);
incomingData.add();
}
template<class T>
void BlockingQueue<T>::push(T && t) {
std::lock_guard lk(m);
queue.push(std::move(t));
incomingData.add();
}
#endif //SUDOKU_BLOCKINGQUEUE_H