diff --git a/utils/threadpool/threadpool.cpp b/utils/threadpool/threadpool.cpp index 40a107424..69db10b17 100644 --- a/utils/threadpool/threadpool.cpp +++ b/utils/threadpool/threadpool.cpp @@ -87,14 +87,15 @@ void ThreadPool::setQueueSize(size_t queueSize) void ThreadPool::pruneThread() { - boost::mutex::scoped_lock lock2(fPruneMutex); + boost::unique_lock lock2(fPruneMutex); while(true) - { - boost::system_time timeout = boost::get_system_time() + boost::posix_time::minutes(1); + { + if (fStop) return; - if (!fPruneThreadEnd.timed_wait(fPruneMutex, timeout)) + if (fPruneThreadEnd.wait_for(lock2, boost::chrono::minutes{1}) == + boost::cv_status::timeout) { while(!fPruneThreads.empty()) { @@ -327,8 +328,7 @@ void ThreadPool::beginThread() throw() utils::setThreadName("Idle"); try { - boost::mutex::scoped_lock lock1(fMutex); - boost::system_time timeout = boost::get_system_time() + boost::posix_time::minutes(10); + boost::unique_lock lock1(fMutex); for (;;) { @@ -347,7 +347,8 @@ void ThreadPool::beginThread() throw() else { // Wait no more than 10 minutes - if (!fNeedThread.timed_wait(lock1, timeout)) // false means it timed out + if (fNeedThread.wait_for(lock1, boost::chrono::minutes{10}) == + boost::cv_status::timeout) { if (fThreadCount > fMaxThreads) { @@ -356,8 +357,6 @@ void ThreadPool::beginThread() throw() --fThreadCount; return; } - - timeout = boost::get_system_time() + boost::posix_time::minutes(10); } } } @@ -390,7 +389,7 @@ void ThreadPool::beginThread() throw() lock1.unlock(); - utils::setThreadName("Unspecified"); + utils::setThreadName("Unspecified"); try { todo->functor(); @@ -434,8 +433,6 @@ void ThreadPool::beginThread() throw() ml.logWarningMessage( message ); } } - - timeout = boost::get_system_time() + boost::posix_time::minutes(10); fThreadAvailable.notify_all(); } } diff --git a/utils/threadpool/threadpool.h b/utils/threadpool/threadpool.h index f0ddf360c..a15adc09f 100644 --- a/utils/threadpool/threadpool.h +++ b/utils/threadpool/threadpool.h @@ -39,10 +39,12 @@ #include #include #include -#include +#include +#include #include #include #include +#include #include @@ -54,7 +56,6 @@ namespace threadpool { - // Taken from boost::thread_group and adapted class ThreadPoolGroup { @@ -351,8 +352,8 @@ private: uint32_t fIssued; boost::mutex fMutex; - boost::condition fThreadAvailable; // triggered when a thread is available - boost::condition fNeedThread; // triggered when a thread is needed + boost::condition_variable fThreadAvailable; // triggered when a thread is available + boost::condition_variable fNeedThread; // triggered when a thread is needed ThreadPoolGroup fThreads; bool fStop; @@ -365,7 +366,7 @@ private: bool fDebug; boost::mutex fInitMutex; boost::mutex fPruneMutex; - boost::condition fPruneThreadEnd; + boost::condition_variable fPruneThreadEnd; boost::thread* fPruneThread; std::stack fPruneThreads; // A list of stale thread IDs to be joined };