-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy paththreadsafequeue.cpp
46 lines (37 loc) · 1.08 KB
/
threadsafequeue.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
#include "threadsafequeue.h"
template<typename T> ThreadSafeQueue<T>::ThreadSafeQueue(const int maxSize):queue_max_size(maxSize)
{
}
template<typename T> ThreadSafeQueue<T>::ThreadSafeQueue(const ThreadSafeQueue &other):queue_max_size(other.queue_max_size)
{
std::lock_guard<std::mutex> lock(mut);
data_queue=other.data_queue;
}
template<typename T> bool ThreadSafeQueue<T>::push(T value)
{
std::lock_guard<std::mutex> lock(mut);
if(data_queue.size()>=queue_max_size)
{
return false;
}
data_queue.push(value);
data_cond.notify_one();
return true;
}
template<typename T> void ThreadSafeQueue<T>::wait_and_pop(T &value)
{
std::unique_lock<std::mutex> u_lock(mut);
data_cond.wait(u_lock,[this]{return !data_queue.empty();});
value=data_queue.front();
data_queue.pop();
}
template<typename T> int ThreadSafeQueue<T>::size() const
{
std::lock_guard<std::mutex> lock(mut);
return data_queue.size();
}
template<typename T> bool ThreadSafeQueue<T>::empty() const
{
std::lock_guard<std::mutex> lock(mut);
return data_queue.empty();
}