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
yielding after a failed poll is rather common when
implementing low-level shared resources or synchronization primitives.
channels for example are implemented using this primitive.
Indeed when you call
recv, which are blocking, they will yield
if the channel is not available.
use std::thread; thread::yield_now();Run