void ConsumerThread(int id) { LOG("Starting consumer thread " << id); for(;;) { { Mutex::Lock __(ItemBufferLock); while(QueueSize == 0 && !StopRequested) ItemBufferNotEmpty.Wait(ItemBufferLock); if(StopRequested && QueueSize == 0) break; int Item = ItemBuffer[QueueStartOffset]; QueueSize--; QueueStartOffset++; TotalItemsConsumed++; if(QueueStartOffset == BUFFER_SIZE) QueueStartOffset = 0; LOG("Consumer " << id << ", item " << Item << ", queue size " << QueueSize); } ItemBufferNotFull.Signal(); Sleep(Random(CONSUMER_SLEEP_TIME_MS)); } LOG("Consumer exiting"); }
void ProducerThread() { LOG("Starting producer thread"); for(;;) { Sleep(Random(PRODUCER_SLEEP_TIME_MS)); { Mutex::Lock __(ItemBufferLock); while(QueueSize == BUFFER_SIZE && !StopRequested) ItemBufferNotFull.Wait(ItemBufferLock); if(StopRequested) break; int Item = Random(1000); ItemBuffer[(QueueStartOffset + QueueSize) % BUFFER_SIZE] = Item; QueueSize++; TotalItemsProduced++; LOG("Producer item " << Item << ", queue size " << QueueSize); } ItemBufferNotEmpty.Signal(); } LOG("Producer exiting"); }
static DWORD WINAPI taskEntry(LPVOID arg) { #else static void *taskEntry(void *arg) { #endif while (true) { workerSemaphore.Wait(); // Try to get task from task queue Task *myTask = NULL; { MutexLock lock(*taskQueueMutex); if (taskQueue.size() == 0) break; myTask = taskQueue.back(); taskQueue.pop_back(); } // Do work for _myTask_ PBRT_STARTED_TASK(myTask); myTask->Run(); PBRT_FINISHED_TASK(myTask); tasksRunningCondition.Lock(); int unfinished = --numUnfinishedTasks; if (unfinished == 0) tasksRunningCondition.Signal(); tasksRunningCondition.Unlock(); } // Cleanup from task thread and exit #ifdef PBRT_HAS_PTHREADS pthread_exit(NULL); #endif // PBRT_HAS_PTHREADS return 0; } #endif // !PBRT_USE_GRAND_CENTRAL_DISPATCH void WaitForAllTasks() { #ifdef PBRT_USE_GRAND_CENTRAL_DISPATCH dispatch_group_wait(gcdGroup, DISPATCH_TIME_FOREVER); #else tasksRunningCondition.Lock(); while (numUnfinishedTasks > 0) tasksRunningCondition.Wait(); tasksRunningCondition.Unlock(); #endif }
int WinMonitorData::SignalCondition( unsigned int cvid ) { int ret = -1; assert( hMutex != NULL ); ConditionVariableMap::iterator iter = cvmap.find( cvid ); if( iter != cvmap.end() ) { ConditionVariable* cv = cvmap[cvid]; assert( cv != NULL ); ret = cv->Signal(); } else { // bad cvid // TODO how to indicate the error? } return ret; }
void OlaServerThread::MarkAsStarted() { m_mutex.Lock(); m_is_running = true; m_mutex.Unlock(); m_condition.Signal(); }