Вопрос

I try to develop a threadpool in C++ and I wonder if it is better to yield() the thread in the main loop of the worker thread or to wait on a condition variable:

void worker_thread( void )
{
    // this is more or less pseudocode
    while( !done )
    {

        if( task_available ) 
             run_task();
        else
            std::this_thread::yield();
    }
}

versus

void worker_thread( void )
{
    // this is more or less pseudocode

    std::unique_lock< std::mutex > lk( mutex_ );
    while( !done )
    {

        if( task_available ) 
             run_task();
        else
            condition_.wait( lk );
    }
}

Any ideas? Will there be any performance differences between both versions?

Это было полезно?

Решение

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.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top