c++ - pthread synchronization on two different conditions -
i have worker thread processing queue of work items. work items might not processable right now, worker thread might push them 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 while (therefore no lock here), // may considered unprocessable if(unprocessable) { scoped_lock(&queuemutex); workqueue.push(item); } } return null; }
now need following: time time, need scan through work queue remove items not needed anymore (from same thread enqueues work items). cannot use queuemutex this, because might miss item being processed, need way pause whole processing thread @ point undone items inside queue (preferrably right @ top of while loop).
i thought second bool variable ("paused") in combination mutex , conditional variable, special case worker waiting signal on queuecondition has handled; pthread_cond_wait()
call have unlock/lock both mutexes.
i guess there must simple solution problem, can't seem able come - hope of able me out.
thanks lot in advance.
basically need emulate winapi's waitformultipleobjects()
call on posix. posix doesn't have single api wait types of events/objects winapi does.
use pthread_cond_timedwait
, clock_gettime
. can refer paper waitfor api many implementation details.
there interesting code there (too post in answer, usable) can solve problem.
p.s. refer question discussion: waitforsingleobject , waitformultipleobjects equivalent in linux
Comments
Post a Comment