GuLearn
GuLearn

Reputation: 1864

Should I use std::this_thread::yield in a busy loop if the latency matters?

I'm trying to implement a low latency event loop with busy spin. But I'm not sure if I should put a std::this_thread::yield in the loop. To be specific:

  1. How does yield "suggest" a reschedule? How often does the context switch actually happen?
  2. What's the main overhead except the context switch it may cause?

Upvotes: 4

Views: 1538

Answers (1)

Mike Seymour
Mike Seymour

Reputation: 254461

Should I use std::this_thread::yield in a busy loop if the latency matters?

No. If the system is busy, then that might stop your thread for a considerable amount of time, breaking your latency requirements.

How does yield "suggest" a reschedule?

It depends on the underlying scheduler. Typically, it maintains one or more queues of threads which want to run; calling yield puts the current thread on the back of a queue, then activates the thread at the front of that queue. If the queue was empty (i.e. there are no more runnable threads than there are processors), then the yielding thread will continue; otherwise, another thread will run, and the yielding thread will wait to be rescheduled.

What's the main overhead except the context switch it may cause?

If the thread continues, there will be no context switch; just the cost of the system call, and a bit of fiddling with the scheduler's queue.

If another thread is scheduled, then you'll have to wait until your thread is rescheduled. This may be unacceptable, depending on your latency requirements.

Upvotes: 6

Related Questions