-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprod_con_queue.cpp
More file actions
47 lines (38 loc) · 909 Bytes
/
prod_con_queue.cpp
File metadata and controls
47 lines (38 loc) · 909 Bytes
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
#include "prod_con_queue.h"
prod_con_queue::prod_con_queue(int max_size)
: max_size(max_size)
{
}
void prod_con_queue::add(std::string item)
{
std::unique_lock<std::mutex> lock(this->mutex);
this->condition.wait(lock, [this]()
{ return !is_full(); });
this->queue.push(item);
lock.unlock();
this->condition.notify_all();
}
std::string prod_con_queue::consume()
{
std::string item;
std::unique_lock<std::mutex> lock(this->mutex);
this->condition.wait(lock, [this]()
{ return !is_empty(); });
item = this->queue.front();
this->queue.pop();
lock.unlock();
this->condition.notify_all();
return item;
}
bool prod_con_queue::is_full() const
{
return this->queue.size() >= this->max_size;
}
bool prod_con_queue::is_empty() const
{
return this->queue.size() == 0;
}
int prod_con_queue::length() const
{
return this->queue.size();
}