Send messages from multiple tasks

This commit is contained in:
Manuel Thalmann 2025-04-06 21:57:17 +02:00
parent 510ecfec6d
commit 519ce8dd85
Signed by: manuth
SSH key fingerprint: SHA256:HsMLC+7kJWALP6YCYCoopxNbUnghwSGLVcG76SECT5c

View file

@ -4,7 +4,8 @@ fn main() {
trpl::run(async {
let (tx, mut rx) = trpl::channel();
let tx_fut = async move {
let tx1 = tx.clone();
let tx1_fut = async move {
let vals = vec![
String::from("hi"),
String::from("from"),
@ -13,17 +14,31 @@ fn main() {
];
for val in vals {
tx.send(val).unwrap();
tx1.send(val).unwrap();
trpl::sleep(Duration::from_millis(500)).await;
}
};
let rx_fut = async {
while let Some(value) = rx.recv().await {
eprintln!("received '{value}'");
println!("received '{value}'");
}
};
trpl::join(tx_fut, rx_fut).await;
let tx_fut = async move {
let vals = vec![
String::from("more"),
String::from("messages"),
String::from("for"),
String::from("you"),
];
for val in vals {
tx.send(val).unwrap();
trpl::sleep(Duration::from_millis(1500)).await;
}
};
trpl::join3(tx1_fut, tx_fut, rx_fut).await;
});
}