views:

294

answers:

3

hello.

To clarify terminology, yield is when thread gives up its time slice. My platform of interest is POSIX threads, but I think question is general.

Suppose I have consumer/producer pattern. If I want to throttle either consumer or producer, which is better to use, sleep or yield? I am mostly interested in efficiency of using either function.

Thanks

+1  A: 

The "right" way to code a producer / consumer is to have the consumer wait for the producer's data. You can achieve this by using a synchronization object such as a Mutex. The consumer will Wait on the mutex, which blocks it from executing until data is available. In turn, the producer will signal the mutex when data is available, which will wake up the consumer thread so it can begin processing. This is more efficient than sleep in terms of both:

  • CPU utilization (no cycles are wasted), and
  • Run Time (execution begins as soon as data is available, not when a thread is scheduled to wake up).

That said, here is an analysis of yield vs sleep that you asked for. You may need to use such a scheme if for some reason waiting for output is not feasible:

It depends how much traffic you are receiving - if data is constantly being received and processed, you might consider doing a yield. However in most cases this will result in a "busy" loop that spends most of its time needlessly waking up the thread to check if anything is ready.

You will probably want to either sleep for a short period of time (perhaps for less than a second, using usleep) OR even better use a synchronization object such as a mutex to signal that data is available.

Justin Ethier
sleep can only suspend execution in seconds, nanosleep can suspend execution in fractions of a second.
Ernelli
@erneli that is implementation details.
aaa
I was referring to Justins recommendation to use sleep for a period of time less than a second. But the correct method is of course to use synchronization.
Ernelli
+1  A: 

One good reason to sleep instead of yield is when there is too much contention at a specific critical section. Lets say for example you try to acquire two locks and there is alot of contention on both locks. Here you can use sleep to employ an exponential backoff. This would allow each failed attempt to pseduo randomly backoff to allow other thread to succeed.

Yielding in this situation doesnt really help as much because the prospect of a random backoff can increase likelyhood that thread starvation would not occur.

Edit: Though I know this isnt necessarily java specific. Java's implementation of Thread.sleep(0) has the same effect of Thread.yield() At that point its more of a matter of style.

John V.
+1  A: 

sleep and yield is not the same. When calling sleep the process/thread gives CPU to another process/thread for the given amount of time.

yield relinquishes the CPU to another thread, but may return immediately if there are no other threads that waits for CPU.

So if you want to throttle, for example when streaming data at regular intervals, then sleep or nanosleep is the function to use.

If synchronization between producer/consumer is needed, you should use a mutex/conditional wait.

Ernelli