I have a worker thread processing a queue of work items. work items might not be processable right now, so the worker thread might push them back into the queue.
void* workerFunc(void* arg) {
WorkItem* item = NULL;
while(true) {
{
scoped_lock(&queueMutex);
while(workerRunning && workQueue.empty())
pthread_cond_wait(&queueCondition, &queueMutex);
if(!workerRunning)
break;
item = workQueue.front();
workQueue.pop();
}
// process item, may take a while (therefore no lock here),
// may also be considered unprocessable
if(unprocessable) {
scoped_lock(&queueMutex);
workQueue.push(item);
}
}
return NULL;
}
Now i need to do the following: from time to time, i need to scan through the work queue to remove items that are not needed anymore (from the same thread that enqueues work items). I cannot use the queueMutex for this, because i might miss the item that is currently being processed, so i need a way to pause the whole processing thread at a point where all undone items are actually inside the queue (preferrably right at the top of the while loop).
I thought about a second bool variable (“paused”) in combination with another mutex and conditional variable, but then the special case where the worker is waiting for a signal on the queueCondition has to be handled; actually the pthread_cond_wait() call would have to unlock/lock both mutexes.
I guess there must be a simple solution to this problem, but i can’t seem to be able to come up with it – I hope some of you will be able to help me out.
Thanks a lot in advance.
Basically you need to emulate WinAPI’s
WaitForMultipleObjects()call on POSIX. POSIX doesn’t have a single API to wait for all types of events/objects as WinAPI does.Use
pthread_cond_timedwaitandclock_gettime. You can refer to this paper WaitFor API for many implementation details.There is some interesting code there (too much to post in the answer, but usable) that can solve your problem.
P.S. Refer to this question for a discussion: WaitForSingleObject and WaitForMultipleObjects equivalent in linux