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
#[macro_use]
extern crate mco;
use std::io::{self, Read, Write};
use std::time::Duration;
use mco::io::CoIo;
// create the io object that can be used in coroutine
// note that we can only access the io ojbect in one thread/coroutin
// this example can't run on windows
// because stdin/stdout can't resiger on IOCP
fn main() {
// run every thing in a single thread to verify the aysnc io
mco::config().set_workers(1);
let b_run = true;
let r = join!(
{
// this will block the whole thread, use Coio instead!!
// let mut stdin = io::stdin();
// the CoIo object will not block the thread.
let mut stdin = CoIo::new(io::stdin()).expect("failed to create stdio");
while b_run {
let mut msg = [0, 4];
stdin.read(&mut msg).expect("failed ot read stdio");
println!("another coroutine, msg={:?}", msg);
}
},
{
let mut stdout = CoIo::new(io::stdout()).expect("failed to create stdout");
while b_run {
write!(stdout, "write from coroutine\n").expect("failed to write");
mco::coroutine::sleep(Duration::from_millis(500));
}
}
);
}