pub struct YieldNow(/* private fields */);Expand description
Future that yields once, then completes.
Returns Pending on first poll, wakes itself, completes on second poll.
Other ready tasks get a turn before this task resumes.
§Caveat: cross-thread waits
yield_now is a cooperative yield within the executor. It does not
park the executor or yield CPU to other OS threads. On a single-threaded
runtime, a tight wait loop like
ⓘ
while !cross_thread_state_ready() {
yield_now().await;
}will busy-spin and starve other OS threads (a tokio worker thread, an Aeron media driver, a separate sender thread) of CPU. The producer can’t fire its wake in time, the loop appears hung even though the external work would have completed eventually.
For cross-thread waits, use a parking primitive instead:
await rx.recv()on a channel — parks until the sender wakesawait notify.notified()on aNotify— parks untilnotify_one()- mix
yield_nowwith periodicsleep— bounded park gives the OS time to schedule producer threads
Trait Implementations§
Auto Trait Implementations§
impl Freeze for YieldNow
impl RefUnwindSafe for YieldNow
impl Send for YieldNow
impl Sync for YieldNow
impl Unpin for YieldNow
impl UnsafeUnpin for YieldNow
impl UnwindSafe for YieldNow
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<F> IntoFuture for Fwhere
F: Future,
impl<F> IntoFuture for Fwhere
F: Future,
Source§type IntoFuture = F
type IntoFuture = F
Which kind of future are we turning this into?
Source§fn into_future(self) -> <F as IntoFuture>::IntoFuture
fn into_future(self) -> <F as IntoFuture>::IntoFuture
Creates a future from a value. Read more