What is the meaning of yield_now
in multi-thread runtime?
#5663
-
use std::sync::{
atomic::{AtomicBool, Ordering::Relaxed},
Arc,
};
use tokio::task;
#[tokio::main]
async fn main() {
let mut succeed = 0;
let mut failed = 0;
for _ in 0..10000 {
if test().await {
succeed += 1;
} else {
failed += 1;
}
}
println!("testing done. succeed {succeed}, failed {failed}");
}
async fn test() -> bool {
let arc = Arc::new(AtomicBool::new(false));
let arc_cloned = arc.clone();
let handle = task::spawn(async move {
// if the spawned task execute later, testing is failed
arc_cloned.store(false, Relaxed);
});
task::yield_now().await;
task::yield_now().await;
// if main task execute later, testing is succeed
arc.store(true, Relaxed);
handle.await.unwrap();
arc.load(Relaxed)
} The result is
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
The main function runs inside a Try executing your test inside the runtime threadpool instead: #[tokio::main]
async fn main() {
tokio::spawn(async move {
let mut succeed = 0;
let mut failed = 0;
for _ in 0..10000 {
if test().await {
succeed += 1;
} else {
failed += 1;
}
}
println!("testing done. succeed {succeed}, failed {failed}");
}).await.unwrap();
}
However, please be warned that this behavior is not guaranteed! If other tasks are running on the runtime, then you may see failures. The |
Beta Was this translation helpful? Give feedback.
The main function runs inside a
block_on
call, which means that it runs outside of the runtime's thread pool. This means that thetokio::spawn
calls are not executed on the same thread as thetest
method, so yielding in the test method does not affect the execution of your spawned tasks.Try executing your test inside the runtime threadpool instead: