Run all asynchronous tasks at the same time

This commit is contained in:
Manuel Thalmann 2025-04-06 00:53:50 +02:00
parent b5625b6c4c
commit 3be0c34179
Signed by: manuth
SSH key fingerprint: SHA256:HsMLC+7kJWALP6YCYCoopxNbUnghwSGLVcG76SECT5c

View file

@ -2,16 +2,20 @@ use std::time::Duration;
fn main() {
trpl::run(async {
trpl::spawn_task(async {
let fut1 = async {
for i in 1..10 {
println!("hi number {i} from the first task!");
trpl::sleep(Duration::from_millis(500)).await;
}
});
};
for i in 1..5 {
println!("hi number {i} from the second task!");
trpl::sleep(Duration::from_millis(500)).await;
}
let fut2 = async {
for i in 1..5 {
println!("hi number {i} from the second task!");
trpl::sleep(Duration::from_millis(500)).await;
}
};
trpl::join(fut1, fut2).await;
});
}