Send messages from multiple tasks
This commit is contained in:
parent
510ecfec6d
commit
519ce8dd85
1 changed files with 19 additions and 4 deletions
|
@ -4,7 +4,8 @@ fn main() {
|
||||||
trpl::run(async {
|
trpl::run(async {
|
||||||
let (tx, mut rx) = trpl::channel();
|
let (tx, mut rx) = trpl::channel();
|
||||||
|
|
||||||
let tx_fut = async move {
|
let tx1 = tx.clone();
|
||||||
|
let tx1_fut = async move {
|
||||||
let vals = vec![
|
let vals = vec![
|
||||||
String::from("hi"),
|
String::from("hi"),
|
||||||
String::from("from"),
|
String::from("from"),
|
||||||
|
@ -13,17 +14,31 @@ fn main() {
|
||||||
];
|
];
|
||||||
|
|
||||||
for val in vals {
|
for val in vals {
|
||||||
tx.send(val).unwrap();
|
tx1.send(val).unwrap();
|
||||||
trpl::sleep(Duration::from_millis(500)).await;
|
trpl::sleep(Duration::from_millis(500)).await;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let rx_fut = async {
|
let rx_fut = async {
|
||||||
while let Some(value) = rx.recv().await {
|
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;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue