Fixed potential shutdown deadlock issue

This commit is contained in:
Peter Thoman 2013-05-01 14:06:29 +02:00
parent 7a22148583
commit c7399857e3

View File

@ -33,10 +33,12 @@ void WorkerThread::WorkFunc() {
started = true; started = true;
while(active) { while(active) {
signal.wait(mutex); signal.wait(mutex);
if(active) work_(); if(active) {
doneMutex.lock(); work_();
done.notify_one(); doneMutex.lock();
doneMutex.unlock(); done.notify_one();
doneMutex.unlock();
}
} }
} }
@ -58,7 +60,7 @@ void ThreadPool::ParallelLoop(std::function<void(int,int)> loop, int lower, int
mutex.lock(); mutex.lock();
StartWorkers(); StartWorkers();
int range = upper-lower; int range = upper-lower;
if(range >= numThreads*2) { // don't parallelize tiny loops if(range >= numThreads*2) { // don't parallelize tiny loops (this could be better, maybe add optional parameter that estimates work per iteration)
// could do slightly better load balancing for the generic case, // could do slightly better load balancing for the generic case,
// but doesn't matter since all our loops are power of 2 // but doesn't matter since all our loops are power of 2
int chunk = range/numThreads; int chunk = range/numThreads;