Threadpool implementation: condition_variables vs. yield() Threadpool implementation: condition_variables vs. yield() multithreading multithreading

Threadpool implementation: condition_variables vs. yield()


if your threads in the thread pool are constantly fed with tasks and you need fast response time, then yield is what you want, but yield will burn cpu cycles no matter what the waiting thread is doing.if not, you can use the conditional approach, threads will sleep until a task is ready (note though, a conditional can wake a thread, even if no ready signal was sent), the response time might be slower, but you will not burn cpu cycles.

i would recommend the conditional approach, and if the reaction time is too slow, switch to yield.


The cost of a thread switch is the same either way. As for whether you should use polling or condition variables, the latter can rest the processor by kicking the thread off until there is really something to do. That results in lower CPU utilization. The polling method will allow the thread to return and "try again", effectively running the CPU indefinitely.

It's worth point-out that there are a few applications that prefer polling, like when task_available is false for very short amounts of time (i.e. there is usually work to do). In that case, you will want to poll task_available in a loop with a counter; only yield the thread when the counter exceeds a threshold.