-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsemaphore-cpp20.cpp
More file actions
31 lines (26 loc) · 1.16 KB
/
semaphore-cpp20.cpp
File metadata and controls
31 lines (26 loc) · 1.16 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
// todo: examples?
// std::semaphore — limit concurrent access to N threads
// std::barrier — reusable count-down based synchronize threads in phases
/*
All threads do their work, then all **wait at the barrier** until every thread arrives — then all resume together, repeating this each phase:
```
phase 1: th1 works... th2 works... th3 works...
[all arrive → barrier releases]
========================== BARRIER ==================================
phase 2: th1 works... th2 works... th3 works...
[all arrive → barrier releases]
========================== BARRIER ==================================
phase 3: ...
```
Like a checkpoint — nobody moves to the next phase until everyone finishes the current one.
*/
// std::latch — one-shot countdown, wait for N threads to finish
// - a barrier with one phase
/*
main: [waiting...]
t1 done → count_down() count: 3→2
t2 done → count_down() count: 2→1
t3 done → count_down() count: 1→0
main: [unblocked, continues] ✓
// latch is now spent — cannot be reused
*/