I'm writing a thread safe queue using C++11 and stl threading. The WaitAndPop method currently looks like the following. I would like to be able to pass something to WaitAndPop that indicates if the calling thread has been asked to stop. WaitAndPop should return true if it waited for and returned an element of the queue, it should return false if the calling thread is being stopped.
bool WaitAndPop(T& value, std::condition_variable callingThreadStopRequested)
{
std::unique_lock<std::mutex> lock(mutex);
while( queuedTasks.empty() )
{
queuedTasksCondition.wait(lock);
}
value = queue.front();
queue.pop_front();
return true;
}
Is it possible to code something like this? I'm used to a Win32 WaitForMultipleObjects, but can't find an alternative that works for this case.
Thanks.
I've seen this related question, but it didn't really answer the problem. learning threads on linux
WaitForMultipleEvents
was usingpoll()
and using pipes for the events. I don't think that's going to help you with your condition variables, however... – trojanfoeWaitForMultipleObjects
in C++ I think. – didierc