pub struct JoinHandle<T> { /* private fields */ }Expand description
Task handle for spawned futures
Implementations§
Source§impl<T> JoinHandle<T>
impl<T> JoinHandle<T>
Sourcepub async fn await_result(self) -> Option<T>
pub async fn await_result(self) -> Option<T>
Wait for the task to complete and return its result
Examples found in repository?
examples/parallel_tasks.rs (line 24)
4fn main() {
5 let rt = Runtime::new();
6
7 rt.block_on(async move {
8 println!("Spawning 100 concurrent tasks...");
9
10 let mut handles = vec![];
11
12 for i in 0..100 {
13 let handle = rt.spawn_with_handle(async move {
14 avx_async::sleep(Duration::from_millis(10)).await;
15 i * i
16 });
17 handles.push(handle);
18 }
19
20 println!("Waiting for all tasks to complete...");
21
22 let mut sum = 0;
23 for handle in handles {
24 if let Some(result) = handle.await_result().await {
25 sum += result;
26 }
27 }
28
29 println!("Sum of squares from 0 to 99: {}", sum);
30 println!("Active tasks: {}", rt.task_count());
31 });
32}