-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path02_ConditionVariableAny.cpp
More file actions
108 lines (80 loc) · 3.41 KB
/
02_ConditionVariableAny.cpp
File metadata and controls
108 lines (80 loc) · 3.41 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// ===========================================================================
// ConditionVariableAny.cpp - std::stop_token, std::stop_source und request_stop
// ===========================================================================
#include "../Logger/Logger.h"
#include <condition_variable> // std::condition_variable_any
#include <iostream> // std::cout
#include <mutex> // std::mutex, std::unique_lock, std::lock_guard
#include <queue> // std::queue
#include <stop_token> // std::stop_token
#include <thread> // std::jthread
#include <string> // std::string
namespace Stop_Tokens_and_Condition_Variables
{
static void test()
{
std::queue<std::string> m_messages;
std::mutex m_mutex;
std::condition_variable_any m_condition_variable;
auto task = [&](std::stop_token token) {
while (!token.stop_requested()) {
std::string msg{};
Logger::log(std::cout, "Waiting ...");
{
std::unique_lock<std::mutex> lock{ m_mutex };
// wait for the next message
bool waitResult {
m_condition_variable.wait(
lock,
token,
[&]() {
bool b{ !m_messages.empty() };
Logger::log(std::cout, "Wait: Queue is empty: ", b ? "false" : "true");
return b;
}
)
};
if (!waitResult) {
Logger::log(std::cout, "Stop has been requested!");
break;
}
// retrieve the next message from the queue
msg = m_messages.front();
m_messages.pop();
}
// print the next message:
Logger::log(std::cout, "Message: ", msg);
}
Logger::log(std::cout, "Leaving JThread");
};
Logger::log(std::cout, "Pushing strings into queue ...");
// store three messages
for (const auto& s : { "Tic" , "Tac", "Toe" }) {
std::lock_guard<std::mutex> guard{ m_mutex };
m_messages.push(s);
}
Logger::log(std::cout, "Starting JThread");
std::jthread t{ task };
std::this_thread::sleep_for(std::chrono::seconds{ 5 });
{
// after some time, store another message
std::lock_guard<std::mutex> guard{ m_mutex };
m_messages.push("Tic-Tac-Toe Done");
}
// notify waiting thread
m_condition_variable.notify_one();
// after some time, end program (requests stop, which interrupts wait())
std::this_thread::sleep_for(std::chrono::seconds{ 5 });
Logger::log(std::cout, "Main Thread: calling request_stop");
t.request_stop();
std::this_thread::sleep_for(std::chrono::seconds{ 2 });
Logger::log(std::cout, "Leaving Main");
}
}
void test_condition_variable_any()
{
Stop_Tokens_and_Condition_Variables::test();
}
// ===========================================================================
// End-of-File
// ===========================================================================