use std::sync::Arc; use tokio::sync::Mutex; #[tokio::main] async fn main() { let counter = Arc::new(Mutex::new(0)); let mut handles = vec![]; for _ in 0..10 { let counter = Arc::clone(&counter); let handle = tokio::spawn(async move { let mut num = counter.lock().await; // 异步锁:用 .await 而不是 unwrap *num += 1; }); handles.push(handle); } for handle in handles { handle.await.unwrap(); } println!("结果: {}", *counter.lock().await); }