-
Notifications
You must be signed in to change notification settings - Fork 0
/
SharedQueue.h
executable file
·49 lines (39 loc) · 1.05 KB
/
SharedQueue.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
//
// SharedQueue.h
// MIPS_Emulator
//
// Created by Matt on 2/5/16.
// Copyright © 2016 Matt. All rights reserved.
//
#ifndef SharedQueue_h
#define SharedQueue_h
#include <queue>
#include <mutex>
template <typename T> class Shared_Queue {
private:
std::queue<T> sharedQueue;
std::mutex queueMutex;
public:
Shared_Queue() {
}
T pop() {
std::lock_guard<std::mutex> lock(queueMutex);
if (!sharedQueue.empty()) {
T result = sharedQueue.front();
sharedQueue.pop();
return result;
}
else {
throw std::runtime_error("Shared Queue is Empty");
}
}
void push(T input) {
std::lock_guard<std::mutex> lock(queueMutex);
sharedQueue.push(input);
}
bool isEmpty() {
std::lock_guard<std::mutex> lock(queueMutex);
return sharedQueue.empty();
}
};
#endif /* SharedQueue_h */