SOLA
Loading...
Searching...
No Matches
solanet
src
network_udp
queue.h
1
// Copyright The SOLA Contributors
2
//
3
// Licensed under the MIT License.
4
// For details on the licensing terms, see the LICENSE file.
5
// SPDX-License-Identifier: MIT
6
7
#ifndef SOLANET_NETWORK_UDP_QUEUE_H_
8
#define SOLANET_NETWORK_UDP_QUEUE_H_
9
10
#include <condition_variable>
11
#include <mutex>
12
#include <queue>
13
14
namespace
solanet {
18
template
<
typename
T>
class
Queue
{
19
public
:
20
~Queue
() {
21
cv_queue_.notify_all();
// Unblock all waiting
22
}
23
28
void
push
(
const
T &value) {
29
{
30
std::scoped_lock lock(queue_mutex_);
31
queue_.push(value);
32
}
33
34
// Notify a thread waiting on pop() that a new message is available
35
cv_queue_.notify_one();
36
}
37
42
void
stop
() {
43
running_ =
false
;
44
cv_queue_.notify_all();
// Unblock all waiting
45
}
46
52
T
pop
() {
53
std::unique_lock lk(queue_mutex_);
54
if
(queue_.empty()) cv_queue_.wait(lk, [
this
] {
return
!running_ || !queue_.empty(); });
55
56
if
(!running_)
return
{};
// Return empty message if we should stop
57
58
// Get first entry, pop it and return
59
T item = queue_.front();
60
queue_.pop();
61
return
item;
62
}
63
64
private
:
65
std::queue<T> queue_;
66
std::mutex queue_mutex_;
67
bool
running_ =
true
;
68
std::condition_variable cv_queue_;
69
};
70
}
// namespace solanet
71
72
#endif
// SOLANET_NETWORK_UDP_QUEUE_H_
solanet::Queue
Definition
queue.h:18
solanet::Queue::stop
void stop()
Definition
queue.h:42
solanet::Queue::push
void push(const T &value)
Definition
queue.h:28
solanet::Queue::pop
T pop()
Definition
queue.h:52
Generated by
1.9.8