From 779ecc08f2968d1aaa3725bf910b1c6c59546ac9 Mon Sep 17 00:00:00 2001 From: Hatter Jiang Date: Sun, 20 Mar 2022 01:20:55 +0800 Subject: [PATCH] feat: add select --- __concurrent/async_study/examples/select.rs | 24 +++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 __concurrent/async_study/examples/select.rs diff --git a/__concurrent/async_study/examples/select.rs b/__concurrent/async_study/examples/select.rs new file mode 100644 index 0000000..818a763 --- /dev/null +++ b/__concurrent/async_study/examples/select.rs @@ -0,0 +1,24 @@ +use std::time::Duration; +use tokio::sync::oneshot::channel; + +#[tokio::main] +async fn main() { + let (tx1, rx1) = channel::(); + let (_tx2, rx2) = channel::(); + + 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"); +} \ No newline at end of file