Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should a thread generally yield?

In most languages/frameworks, there exists a way for a thread to yield control to other threads. However, I can't really think of a time when yielding from a thread was the correct solution to a given problem. When, in general, should one use Thread.yield(), sleep(0), etc?

like image 669
Jason Baker Avatar asked Feb 01 '13 20:02

Jason Baker


People also ask

Why would a thread ever want to call yield?

The yield() basically means that the thread is not doing anything particularly important and if any other threads or processes need to be run, they should run. Otherwise, the current thread will continue to run.

Under what circumstances might you use the yield method of the thread class?

If in case there are no waiting threads or if all the waiting threads have low priority then the same thread will continue its execution. The advantage of yield() method is to get a chance to execute other waiting threads so if our current thread takes more time to execute and allocate processor to other threads.

What does yielding a thread mean?

In computer science, yield is an action that occurs in a computer program during multithreading, of forcing a processor to relinquish control of the current running thread, and sending it to the end of the running queue, of the same scheduling priority.

What happens when a thread calls yield?

Whenever a thread calls the Thread. yield() method, it gives a hint to the thread scheduler that it is ready to pause its execution.


1 Answers

One use case could be for testing concurrent programs, try to find interleavings that reveal flaws in your synchronization patterns. For instance in Java:

A useful trick for increasing the number of interleavings, and therefore more effectively exploring the state space of your programs, is to use Thread.yield to encourage more context switches during operations that access shared state. (The effectiveness of this technique is platform-specific, since the JVM is free to treat THRead.yield as a no-op [JLS 17.9]; using a short but nonzero sleep would be slower but more reliable.) — JCIP

Also interesting from the Java point of view is that their semantics are not defined:

The semantics of Thread.yield (and Thread.sleep(0)) are undefined [JLS 17.9]; the JVM is free to implement them as no-ops or treat them as scheduling hints. In particular, they are not required to have the semantics of sleep(0) on Unix systemsput the current thread at the end of the run queue for that priority, yielding to other threads of the same prioritythough some JVMs implement yield in this way. — JCIP

This makes them, of course, rather unreliable. This is very Java specific, however, in generally I believe following is true:

Both are low-level mechanism which can be used to influence the scheduling order. If this is used to achieve a certain functionality then this functionality is based on the probability of the OS scheduler which seems a rather bad idea. This should be managed by higher-level synchronization constructs instead.

For testing purpose or for forcing the program into a certain state it seems a handy tool.

like image 131
Konrad Reiche Avatar answered Sep 23 '22 23:09

Konrad Reiche