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
/*******************************************************************************
* tests/semaphore_test.cpp
*
* Part of tlx - http://panthema.net/tlx
*
* Copyright (C) 2018 Timo Bingmann <tb@panthema.net>
*
* All rights reserved. Published under the Boost Software License, Version 1.0
******************************************************************************/
#include <tlx/semaphore.hpp>
#include <tlx/die.hpp>
#include <thread>
static void test_semaphore() {
static const size_t limit = 16;
tlx::Semaphore sem;
std::thread t1 = std::thread(
[&]() {
for (size_t i = 0; i < limit; ++i) {
sem.signal(i);
}
sem.signal();
});
std::thread t2 = std::thread(
[&]() {
for (size_t i = 0; i < limit; ++i) {
sem.wait(i);
}
sem.wait();
});
t1.join();
t2.join();
}
int main() {
test_semaphore();
return 0;
}
/******************************************************************************/