WebCFace 2.5.2
Web-based Communication Framework & Dashboard-like UI
Loading...
Searching...
No Matches
queue.h
Go to the documentation of this file.
1#pragma once
2#ifdef WEBCFACE_MESON
3#include "webcface-config.h"
4#else
5#include "webcface/common/webcface-config.h"
6#endif
7#include <queue>
8#include <mutex>
9#include <condition_variable>
10#include <chrono>
11#include <optional>
12
18template <typename T>
19class Queue {
20 std::mutex mtx;
21 std::condition_variable cond;
22 std::queue<T> que;
23
24 public:
25 void push(const T &f) {
26 std::lock_guard lock(mtx);
27 que.push(f);
28 cond.notify_all();
29 }
30 void push(T &&f) {
31 std::lock_guard lock(mtx);
32 que.push(std::move(f));
33 cond.notify_all();
34 }
35 bool empty() {
36 std::lock_guard lock(mtx);
37 return que.empty();
38 }
39 std::optional<T> pop() {
40 std::lock_guard lock(mtx);
41 if (!que.empty()) {
42 auto c = que.front();
43 que.pop();
44 return c;
45 }
46 return std::nullopt;
47 }
48 std::optional<T> pop(std::chrono::milliseconds d) {
49 std::unique_lock lock(mtx);
50 if (cond.wait_for(lock, d, [&] { return !que.empty(); })) {
51 auto c = que.front();
52 que.pop();
53 return c;
54 }
55 return std::nullopt;
56 }
57 void clear() {
58 std::lock_guard lock(mtx);
59 std::queue<T>().swap(que);
60 }
61};
排他制御をしたただのキュー
Definition queue.h:19
void push(const T &f)
Definition queue.h:25
bool empty()
Definition queue.h:35
std::optional< T > pop()
Definition queue.h:39
void clear()
Definition queue.h:57
void push(T &&f)
Definition queue.h:30
std::optional< T > pop(std::chrono::milliseconds d)
Definition queue.h:48
#define WEBCFACE_NS_END
Definition webcface-config.h:104
#define WEBCFACE_NS_BEGIN
Definition webcface-config.h:103