-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSemaphore_03.cpp
More file actions
128 lines (87 loc) · 3.48 KB
/
Semaphore_03.cpp
File metadata and controls
128 lines (87 loc) · 3.48 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// ===========================================================================
// Semaphore // Semaphore_03.cpp
// ===========================================================================
#include "../Logger/Logger.h"
#include <chrono>
#include <iostream>
#include <mutex>
#include <print>
#include <queue>
#include <semaphore>
#include <thread>
namespace ConcurrencyCountingSemaphore {
static void test_counting_semaphore_01()
{
std::mutex mutex{};
// initialize a queue with multiple sequences from ’A’ to ’Z’
std::queue<char> values{};
for (std::size_t i{}; i != 1000; ++i) {
char ch{ static_cast < char>('A' + (i % ('Z' - 'A'))) };
values.push(ch);
}
constexpr int numThreads{ 8 };
std::counting_semaphore<numThreads> enabled{ 0 };
// create a thread pool
std::vector<std::jthread> pool{};
auto procedure = [&](std::stop_token token, int n) {
std::thread::id tid{ std::this_thread::get_id() };
Logger::log(std::cout, "> tid: ", tid);
while (!token.stop_requested()) {
// request this thread to become one of the enabled threads
enabled.acquire();
// get next value from the queue:
char ch{};
{
std::lock_guard<std::mutex> guard{ mutex };
ch = values.front();
values.pop();
}
// print the value 10 times
for (int i{}; i != 10; ++i) {
if (token.stop_requested()) {
break;
}
std::print("{}", ch);
auto duration{ std::chrono::milliseconds{ 300 } *((n % 3) + 1) };
std::this_thread::sleep_for(std::chrono::milliseconds{ duration });
}
// remove thread from the set of enabled threads
enabled.release();
}
Logger::log(std::cout, "< tid: ", tid);
};
// create and start all threads of the pool
for (int i{}; i != numThreads; ++i) {
std::jthread t{ procedure, i };
pool.push_back(std::move(t));
}
// now play with the threads
Logger::log(std::cout, "> wait 2 seconds (no thread enabled)");
std::this_thread::sleep_for(std::chrono::seconds{ 2 });
// enable 4 concurrent threads:
Logger::log(std::cout, "\n> enable 4 parallel threads");
enabled.release(4);
std::this_thread::sleep_for(std::chrono::seconds{ 5 });
// enable 4 more concurrent threads
Logger::log(std::cout, "\n> enable 4 more parallel threads");
enabled.release(4);
std::this_thread::sleep_for(std::chrono::seconds{ 5 });
// we could run forever, but let’s end the program here:
Logger::log(std::cout, "\n> stop processing");
for (auto& t : pool) {
t.request_stop();
}
Logger::log(std::cout, "\n> wait for end of threads");
for (auto& t : pool) {
t.join();
}
Logger::log(std::cout, "\n> Done.");
}
}
void test_counting_semaphore() {
using namespace ConcurrencyCountingSemaphore;
test_counting_semaphore_01();
}
// ===========================================================================
// End-of-File
// ===========================================================================