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.