Skip to content

Instantly share code, notes, and snippets.

@thorseraq
Last active April 11, 2023 10:22
Show Gist options
  • Select an option

  • Save thorseraq/3f366e5aff2ed2f2b17d2954071840ce to your computer and use it in GitHub Desktop.

Select an option

Save thorseraq/3f366e5aff2ed2f2b17d2954071840ce to your computer and use it in GitHub Desktop.
use std::sync::Arc;
use std::thread::sleep;
use std::time::Duration;
use tokio::sync::RwLock;
#[tokio::main]
async fn main() {
let (tx, mut rx) = tokio::sync::mpsc::channel::<i32>(100);
for i in 0..100 {
tx.send(i).await.expect("TODO: panic message");
}
let rx = Arc::new(RwLock::new(rx));
{
let rx = rx.clone();
tokio::spawn(async move {
while let Some(message) = rx.write().await.recv().await {
println!("GOT {}", message);
}
});
}
sleep(Duration::from_secs(10));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment