forked from Xudong-Huang/may
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeneral_io.rs
38 lines (33 loc) · 1.2 KB
/
general_io.rs
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
#[macro_use]
extern crate may;
use std::io::{self, Read, Write};
use std::time::Duration;
use may::io::CoIo;
// create the io object that can be used in coroutine
// note that we can only access the io object in one thread/coroutine
// this example can't run on windows
// because stdin/stdout can't register on IOCP
fn main() {
// run every thing in a single thread to verify the async io
may::config().set_workers(1);
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");
loop {
let mut msg = [0, 4];
let _n = 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");
loop {
writeln!(stdout, "write from coroutine").expect("failed to write");
may::coroutine::sleep(Duration::from_millis(500));
}
}
);
}