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
70
71
72
73
74
75
76
77
use std::time::Duration;
use mco::{co};
fn main() {
println!("thread {:?}", std::thread::current().id());
co!(|| {
println!("coroutine run on thread {:?}", std::thread::current().id());
});
std::thread::sleep(Duration::from_secs(2));
// use mco::coroutine::{ spawn, yield_now, Builder, Spawn};
// co!(2 * 4096, || {
// println!(
// "coroutine with stack size: {}",
// mco::coroutine::current().stack_size()
// );
// });
// (2 * 4096).spawn(|| {
// println!(
// "coroutine with stack size: {}",
// mco::coroutine::current().stack_size()
// );
// });
// co!("coroutine", || {
// println!(
// "coroutine with name: {}",
// mco::coroutine::current().name().unwrap_or_default()
// );
// });
// "coroutine".spawn(|| {
// println!(
// "coroutine with name: {}",
// mco::coroutine::current().name().unwrap_or_default()
// );
// });
// co!(Builder::new(), || {
// println!("coroutine with Builder");
// });
// Builder::new().spawn(|| {
// println!("coroutine with Builder::spawn");
// });
// spawn(|| {
// println!("coroutine with method spawn");
// });
// //yield example
// co!(move || {
// println!("hi, I'm parent");
// let v = (0..100)
// .map(|i| {
// co!(move || {
// println!("hi, I'm child{:?}", i);
// yield_now();
// println!("bye from child{:?}", i);
// })
// })
// .collect::<Vec<_>>();
// yield_now();
// // wait child finish
// for i in v {
// i.join().unwrap();
// }
// println!("bye from parent");
// })
// .join()
// .unwrap();
//
// mco::coroutine::sleep(use std::time::Duration::from_secs(1));
// //cancel example
// let g = co!(|| {
// mco::defer!(|| { println!("cancel done!") });
// for idx in 0..1000 {
// mco::coroutine::sleep(use std::time::Duration::from_secs(1));
// println!("{}", idx);
// }
// });
//std::thread::sleep(use std::time::Duration::from_secs(2));
// g.coroutine().cancel();
}