diff --git a/src/ThreadPool.cpp b/src/ThreadPool.cpp index dda524e73..baea9ef07 100644 --- a/src/ThreadPool.cpp +++ b/src/ThreadPool.cpp @@ -13,6 +13,7 @@ ThreadPool::ThreadPool() : maxThreads(1000), die(false), threadsWaiting(0) // context it's used in. In the CRP class for example, the # of active threads would be // naturally limited by the # of concurrent operations. logger = SMLogging::get(); + pruner = boost::thread([this] { this->prune(); } ); } ThreadPool::ThreadPool(uint num_threads, bool _processQueueOnExit) : maxThreads(num_threads), die(false), @@ -62,8 +63,9 @@ void ThreadPool::prune() { while (pruneable.empty() && !die) somethingToPrune.wait(s); + if (die) - return; + break; for (auto &id : pruneable) { @@ -83,6 +85,12 @@ void ThreadPool::setMaxThreads(uint newMax) maxThreads = newMax; } +int ThreadPool::currentQueueSize() const +{ + boost::unique_lock s(mutex); + return jobs.size(); +} + void ThreadPool::processingLoop() { try diff --git a/src/ThreadPool.h b/src/ThreadPool.h index 32cb72f31..2d75d522f 100644 --- a/src/ThreadPool.h +++ b/src/ThreadPool.h @@ -27,6 +27,7 @@ class ThreadPool : public boost::noncopyable void addJob(const boost::shared_ptr &j); void setMaxThreads(uint newMax); + int currentQueueSize() const; private: void processingLoop(); // the fcn run by each thread @@ -57,7 +58,7 @@ class ThreadPool : public boost::noncopyable boost::condition jobAvailable; std::deque > jobs; - boost::mutex mutex; + mutable boost::mutex mutex; const boost::posix_time::time_duration idleThreadTimeout = boost::posix_time::seconds(60); boost::thread pruner;