feat: add select
This commit is contained in:
24
__concurrent/async_study/examples/select.rs
Normal file
24
__concurrent/async_study/examples/select.rs
Normal file
@@ -0,0 +1,24 @@
|
||||
use std::time::Duration;
|
||||
use tokio::sync::oneshot::channel;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
let (tx1, rx1) = channel::<i32>();
|
||||
let (_tx2, rx2) = channel::<i32>();
|
||||
|
||||
tokio::spawn(async move {
|
||||
tokio::time::sleep(Duration::from_millis(200)).await;
|
||||
tx1.send(1);
|
||||
});
|
||||
|
||||
println!("start select");
|
||||
tokio::select! {
|
||||
val = rx1 => {
|
||||
println!("rx1 completed first with {:?}", val);
|
||||
}
|
||||
val = rx2 => {
|
||||
println!("rx2 completed first with {:?}", val);
|
||||
}
|
||||
}
|
||||
println!("end select");
|
||||
}
|
||||
Reference in New Issue
Block a user