-
Notifications
You must be signed in to change notification settings - Fork 0
/
lbos.cc
182 lines (145 loc) · 4.2 KB
/
lbos.cc
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
Copyright 2021 Simon (psyomn) Symeonidis
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/* This is me writing code to accompany my reading of the little book
* of semaphores. The book is free and very good. Read it.
*/
#include <atomic>
#include <chrono>
#include <iostream>
#include <mutex>
#include <string>
#include <thread>
#include <vector>
#include <unistd.h>
class Semaphore {
public:
explicit Semaphore(std::int64_t s) : value_(s) {}
Semaphore(std::int64_t s, std::int64_t m) : value_(s), max_(m) {}
Semaphore(const Semaphore& other) = delete;
Semaphore(Semaphore&& other) = delete;
Semaphore& operator=(Semaphore& other) = delete;
Semaphore& operator=(Semaphore&& other) = delete;
~Semaphore() {}
void signal(void) {
value_.fetch_add(1, std::memory_order_relaxed);
}
void wait(void) const {
// block until we get signaled. yield for semi spinlock
while (value_.load() <= 0) std::this_thread::yield();
}
private:
std::atomic<int64_t> value_;
std::atomic<int64_t> max_;
};
namespace section_1_5_2 {
void counters_atomic(void);
void counters_mutex(void);
void add(std::string label, int& cnt, std::mutex& mm);
void counters_atomic(void) {
std::atomic<uint64_t> counter;
auto fn = [&counter]() noexcept {
for (size_t i = 0; i < 100; ++i)
counter.fetch_add(1, std::memory_order_relaxed);
};
std::thread t1(fn), t2(fn);
t1.join();
t2.join();
std::cout
<< "counters (atomic) result (should be 200): "
<< counter.load() << std::endl;
}
void add(std::string label, int& cnt, std::mutex& mm) {
(void) label;
for (size_t i = 0; i < 1000; ++i) {
const std::lock_guard<std::mutex> lock(mm);
// std::cout << label << " ";
cnt = cnt + 1;
std::this_thread::yield();
}
}
void counters_mutex(void) {
int counter = 0;
std::mutex m;
std::thread
t1(add, "a", std::ref(counter), std::ref(m)),
t2(add, "b", std::ref(counter), std::ref(m));
t1.join();
t2.join();
std::cout
<< "counters (mutex) result (should be 2000): "
<< counter << std::endl;
}
}
namespace section_3_3 {
void rendez_vous(void);
void rendez_vous(void) {
Semaphore sa(0), sb(0);
std::thread ta([&sa, &sb](){
std::cout << "thread a, statement 1" << std::endl;
sb.signal();
sa.wait();
std::cout << "thread a, statement 2" << std::endl;
});
std::thread tb([&sa, &sb](){
std::cout << "thread b, statement 1" << std::endl;
sb.wait();
sa.signal();
std::cout << "thread b, statement 2" << std::endl;
});
ta.join();
tb.join();
}
}
namespace section_3_5 {
void multiplex(void);
void multiplex() {
constexpr size_t num_threads = 100;
constexpr size_t max_threads = 5;
std::uint64_t counter = 0;
Semaphore sem(max_threads);
std::vector<std::thread> threads;
auto fn = [&sem, &counter]() noexcept {
sem.wait();
counter++;
sem.signal();
};
for (size_t i = 0; i < num_threads; ++i)
threads.push_back(std::thread(fn));
for (auto& e : threads)
e.join();
}
}
int main(int argc, char *argv[])
{
int opt;
while ((opt = getopt(argc, argv, "ab")) != -1) {
switch (opt) {
case 'a':
section_1_5_2::counters_atomic();
section_1_5_2::counters_mutex();
break;
case 'b':
section_3_3::rendez_vous();
break;
default: /* '?' */
fprintf(stderr,
"Usage: %s <-ab> \n"
" -a run section 1.5.x examples \n"
" -b run section 3.3.x rendez vous examples\n"
" -c run section 3.5.x multiplexer\n"
, argv[0]);
exit(EXIT_FAILURE);
}
}
return EXIT_SUCCESS;
}