-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwait_group.cpp
More file actions
48 lines (40 loc) · 1.17 KB
/
Copy pathwait_group.cpp
File metadata and controls
48 lines (40 loc) · 1.17 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
/*
* The [WaitGroup][] type from the [sync][] package is implemented. This is a
* naive implementation using a mutex and a condition variable. A possible
* future improvement might use a lockless algorithm.
*
* This example is somewhat pointless since joining the threads accomplishes
* the same result, however it illustrates how a wait group might be used, for
* example, to wait for results from a thread pool.
*
* [WaitGroup]: https://golang.org/pkg/sync/#WaitGroup
* [sync]: https://golang.org/pkg/sync/
*/
#include <chrono>
#include <iostream>
#include <thread>
#include <vector>
#include <bongo/sync.h>
using namespace std::chrono_literals;
int main() try {
bongo::sync::wait_group wg;
std::vector<std::thread> threads;
for (long i = 0; i < 5; ++i) {
wg.add(1);
threads.emplace_back([&](long i) {
std::cout << "Worker " << i << " starting\n";
std::this_thread::sleep_for(1s);
std::cout << "Worker " << i << " done\n";
wg.done();
}, i);
}
wg.wait();
printf("All workers done\n");
for (auto& t : threads) {
t.join();
}
return 0;
} catch (std::exception const& e) {
std::cerr << e.what() << "\n";
return 1;
}