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
use mco::std::sync::WaitGroup;
use std::time::Duration;
fn main() {
let wg = WaitGroup::new();
//wait thread
for _ in 0..4 {
// Create another reference to the wait group.
let wg = wg.clone();
std::thread::spawn(move || {
// Do some work
println!("sleep 1s");
mco::coroutine::sleep(Duration::from_secs(1));
// Drop the reference to the wait group.
drop(wg);
});
}
//wait coroutines
for _ in 0..4 {
// Create another reference to the wait group.
let wg = wg.clone();
mco::co!(move || {
// Do some work.
println!("sleep 1s");
mco::coroutine::sleep(Duration::from_secs(1));
// Drop the reference to the wait group.
drop(wg);
});
}
// Block until all threads have finished their work.
wg.wait();
println!("all done!");
}