-
Notifications
You must be signed in to change notification settings - Fork 0
/
workerQueue.cpp
43 lines (37 loc) · 1 KB
/
workerQueue.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
#include "workerQueue.hpp"
WorkerQueue::WorkerQueue() {
}
WorkerQueue& WorkerQueue::getInstance() {
static WorkerQueue instance;
return instance;
}
void WorkerQueue::add(std::shared_ptr<WorkItem> item) {
{
std::lock_guard<std::mutex> lock_wQueue(mtx_);
wQueue_.push(item);
std::cout << "Producer added an item" << std::endl;
}
cv_.notify_one();
}
std::shared_ptr<WorkItem> WorkerQueue::remove() {
std::shared_ptr<WorkItem> item;
{
std::unique_lock<std::mutex> wait_wQueue(mtx_);
while(wQueue_.empty()) {
cv_.wait(wait_wQueue);
}
item = wQueue_.front();
wQueue_.pop();
std::cout << "Removing an item." << "Message: " << item->getMessage() <<
" Number: " << item->getNumber() << std::endl;
}
return item;
}
int WorkerQueue::size() {
int result = 0;
{
std::lock_guard<std::mutex> lock_wQueue(mtx_);
result = wQueue_.size();
}
return result;
}