Make sure we re-acquire lock if a task throws

This commit is contained in:
Casey Rodarmor 2015-08-17 17:30:46 -04:00
parent 1e92b27540
commit fb08d92312
1 changed files with 7 additions and 5 deletions

View File

@ -6,6 +6,7 @@
#include <assert.h> #include <assert.h>
#include <boost/bind.hpp> #include <boost/bind.hpp>
#include <boost/thread/reverse_lock.hpp>
#include <utility> #include <utility>
CScheduler::CScheduler() : nThreadsServicingQueue(0), stopRequested(false), stopWhenEmpty(false) CScheduler::CScheduler() : nThreadsServicingQueue(0), stopRequested(false), stopWhenEmpty(false)
@ -65,11 +66,12 @@ void CScheduler::serviceQueue()
Function f = taskQueue.begin()->second; Function f = taskQueue.begin()->second;
taskQueue.erase(taskQueue.begin()); taskQueue.erase(taskQueue.begin());
// Unlock before calling f, so it can reschedule itself or another task {
// without deadlocking: // Unlock before calling f, so it can reschedule itself or another task
lock.unlock(); // without deadlocking:
f(); boost::reverse_lock<boost::unique_lock<boost::mutex> > rlock(lock);
lock.lock(); f();
}
} catch (...) { } catch (...) {
--nThreadsServicingQueue; --nThreadsServicingQueue;
throw; throw;