-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSafeQueue.cpp
More file actions
43 lines (36 loc) · 825 Bytes
/
Copy pathSafeQueue.cpp
File metadata and controls
43 lines (36 loc) · 825 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
//
// Created by papraczy on 05/11/2019.
//
#include <iostream>
#include "SafeQueue.h"
//template <class T>
SafeQueue::SafeQueue(void)
{
}
//template <class T>
SafeQueue::~SafeQueue(void)
{
}
// Add an element to the queue.
//template <class T>
void SafeQueue::push(string& item)
{
std::unique_lock<std::mutex> mlock(mutex_);
this->queue_.push(item);
mlock.unlock(); // unlock before notificiation to minimize mutex contention
cond_.notify_one(); // notify one waiting thread
}
// Get the "front"-element.
// If the queue is empty, wait till a element is avaiable.
//template <class T>
string SafeQueue::pop(void)
{
std::unique_lock<std::mutex> mlock(mutex_);
while (queue_.empty())
{
cond_.wait(mlock);
}
auto item = queue_.front();
queue_.pop();
return item;
}