Function std::thread::yield_now 1.0.0[−][src]
pub fn yield_now()
Cooperatively gives up a timeslice to the OS scheduler.
This is used when the programmer knows that the thread will have nothing to do for some time, and thus avoid wasting computing time.
For example when polling on a resource, it is common to check that it is available, and if not to yield in order to avoid busy waiting.
Thus the pattern of yield
ing after a failed poll is rather common when
implementing low-level shared resources or synchronization primitives.
However programmers will usually prefer to use, channel
s, Condvar
s,
Mutex
es or join
for their synchronization routines, as they avoid
thinking about thread scheduling.
Note that channel
s for example are implemented using this primitive.
Indeed when you call send
or recv
, which are blocking, they will yield
if the channel is not available.
Examples
use std::thread; thread::yield_now();Run