deps/unrar/threadpool.cpp
// Start queued tasks and wait until all threads are inactive.
// We assume that it is always called from main thread, when pool threads
// are sleeping yet.
Start queued tasks and wait until all threads are inactive.
We assume that it is always called from main thread, when pool threads
are sleeping yet.
Start queued tasks and wait until all threads are inactive . We assume that it is always called from main thread when pool threads are sleeping yet .
void ThreadPool::WaitDone()
{
  if (ActiveThreads==0)
    return;
#ifdef _WIN_ALL
  ResetEvent(NoneActive);
  ReleaseSemaphore(QueuedTasksCnt,ActiveThreads,NULL);
  CWaitForSingleObject(NoneActive);
#elif defined(_UNIX)
  AnyActive=true;
  pthread_mutex_lock(&QueuedTasksCntMutex);
  QueuedTasksCnt+=ActiveThreads;
  pthread_mutex_unlock(&QueuedTasksCntMutex);
  pthread_cond_broadcast(&QueuedTasksCntCond);
  pthread_mutex_lock(&AnyActiveMutex);
  while (AnyActive)
    cpthread_cond_wait(&AnyActiveCond,&AnyActiveMutex);
  pthread_mutex_unlock(&AnyActiveMutex);
#endif
}
void ThreadPool :: WaitDone ( ) { if ( ActiveThreads == 0 ) return ; #ifdef _WIN_ALL ResetEvent ( NoneActive ) ; ReleaseSemaphore ( QueuedTasksCnt , ActiveThreads , NULL ) ; CWaitForSingleObject ( NoneActive ) ; #elif defined ( _UNIX ) AnyActive = true ; pthread_mutex_lock ( & QueuedTasksCntMutex ) ; QueuedTasksCnt += ActiveThreads ; pthread_mutex_unlock ( & QueuedTasksCntMutex ) ; pthread_cond_broadcast ( & QueuedTasksCntCond ) ; pthread_mutex_lock ( & AnyActiveMutex ) ; while ( AnyActive ) cpthread_cond_wait ( & AnyActiveCond , & AnyActiveMutex ) ; pthread_mutex_unlock ( & AnyActiveMutex ) ; #endif }
Start queued tasks and wait until all threads are inactive.
Start queued tasks and wait until all threads are inactive .
// Threads reset AnyActive before accessing QueuedTasksCnt and even // preceding WaitDone() call does not guarantee that some slow thread // is not accessing QueuedTasksCnt now. So lock is necessary.