-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbarrier.cpp
More file actions
69 lines (63 loc) · 1.93 KB
/
Copy pathbarrier.cpp
File metadata and controls
69 lines (63 loc) · 1.93 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
/******************************************************************************
* Compilation: g++ -o barrier.cpp -std=c++11 -g
* Execution: ./a.out
* Dependencies:
*
* a synchronization mechanism that causes threads to wait until
* the required number of threads has reached the barrier.
* Once all the threads have reached the barrier, they’re all
* unblocked and may proceed.
*
* % ./a.out
* this thread: 0x700008f75000 sleep for 650 ms
* this thread: 0x700008ff8000 sleep for 886 ms
* this thread: 0x70000907b000 sleep for 992 ms
* this thread: 0x700008f75000 finished.
* this thread: 0x700008ff8000 finished.
* this thread: 0x70000907b000 finished.
*
******************************************************************************/
#include <cstdlib>
#include <thread>
#include <chrono>
#include <atomic>
#include <iostream>
class barrier {
unsigned int const count;
std::atomic<unsigned int> spaces;
std::atomic<unsigned int> generation;
public:
explicit barrier(unsigned int const count_) :
count(count_), spaces(count), generation(0)
{}
void wait() {
unsigned int const my_generation = generation;
if (--spaces == 0) {
spaces = count;
++generation;
} else {
while (my_generation == generation)
std::this_thread::yield();
}
}
};
void fun(barrier* bar) {
std::srand(std::time(0));
int rand_ms = std::rand() % 1000;
std::this_thread::sleep_for(std::chrono::milliseconds(rand_ms));
std::cout << "this thread: " << std::this_thread::get_id() << " sleep for "
<< rand_ms << " ms" << std::endl;
bar->wait();
std::this_thread::sleep_for(std::chrono::milliseconds(rand_ms));
std::cout << "this thread: " << std::this_thread::get_id()
<< " finished."<< std::endl;
}
int main(int argc, char* argv[]) {
barrier bar(3);
std::thread t1(fun, &bar);
std::thread t2(fun, &bar);
std::thread t3(fun, &bar);
t1.join();
t2.join();
t3.join();
}