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?