-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathasync_latch.cpp
More file actions
43 lines (36 loc) · 1.17 KB
/
Copy pathasync_latch.cpp
File metadata and controls
43 lines (36 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
// Adapted from https://en.cppreference.com/w/cpp/thread/latch.html#Example
#include <coio/core.h>
#include <coio/sync_primitives.h>
#include "common.h"
struct Job {
std::string name;
std::string product;
};
auto work(Job& job, coio::async_latch<>& work_done, coio::async_latch<>& start_clean_up) -> coio::task<> {
job.product = job.name + " worked";
work_done.count_down();
co_await start_clean_up.wait();
job.product = job.name + " cleaned";
}
auto main() -> int {
Job jobs[]{{"Annika"}, {"Buru"}, {"Chuck"}};
coio::async_latch<> work_done{std::ranges::size(jobs)};
coio::async_latch<> start_clean_up{1};
coio::async_scope scope;
::print("Work is starting... ");
for (auto& job : jobs) {
scope.spawn(work(job, work_done, start_clean_up));
}
coio::this_thread::sync_wait(work_done.wait());
::println("done:");
for (const auto& job : jobs) {
::println(" {}", job.product);
}
::print("Workers are cleaning up... ");
start_clean_up.count_down();
coio::this_thread::sync_wait(scope.join());
::println("done:");
for (const auto& job : jobs) {
::println(" {}", job.product);
}
}