ฉันกำลังพยายามที่จะหนีสองคนฟังก์ชันอยู่ควบคู่กับ join
.
ของรหัสคือธรรมดา:
tokio = { version = "1.14.0", features = ["full"] }
use tokio::join;
use std::thread::sleep;
use std::time::{Duration, Instant};
async fn fn_1() -> i8 {
sleep(Duration::from_secs(2));
2
}
async fn fn_2() -> i8 {
sleep(Duration::from_secs(2));
1
}
#[tokio::main]
async fn main() -> () {
let now = Instant::now();
println!("start: {:#?}", now.elapsed());
let a = fn_1();
let b = fn_2();
join!(a, b);
println!("end: {:#?}", now.elapsed());
}
แต่ไม่ว่าสิ่งที่ฉันทำอย่างนี้ต้องใช้ 4s—2s + 2s
—ในขณะที่มันควรจะเอา 2s ถ้าผมไม่ผิดพลาด:
start: 37ns
end: 4.01036111s
มีบางอย่างฉันหายไปงั้นเหรอ?