Skip to content
Snippets Groups Projects
Commit a2ba0e63 authored by Thomas Müller's avatar Thomas Müller
Browse files

`ThreadPool` simplifications

parent df85fe95
No related branches found
No related tags found
No related merge requests found
...@@ -41,8 +41,6 @@ public: ...@@ -41,8 +41,6 @@ public:
auto enqueueTask(F&& f, bool highPriority = false) -> std::future<std::result_of_t <F()>> { auto enqueueTask(F&& f, bool highPriority = false) -> std::future<std::result_of_t <F()>> {
using return_type = std::result_of_t<F()>; using return_type = std::result_of_t<F()>;
++mNumTasksInSystem;
auto task = std::make_shared<std::packaged_task<return_type()>>(std::forward<F>(f)); auto task = std::make_shared<std::packaged_task<return_type()>>(std::forward<F>(f));
auto res = task->get_future(); auto res = task->get_future();
...@@ -63,13 +61,8 @@ public: ...@@ -63,13 +61,8 @@ public:
void startThreads(size_t num); void startThreads(size_t num);
void shutdownThreads(size_t num); void shutdownThreads(size_t num);
void setNThreads(size_t num);
size_t numTasksInSystem() const {
return mNumTasksInSystem;
}
void waitUntilFinished();
void waitUntilFinishedFor(const std::chrono::microseconds Duration);
void flushQueue(); void flushQueue();
template <typename Int, typename F> template <typename Int, typename F>
...@@ -109,10 +102,6 @@ private: ...@@ -109,10 +102,6 @@ private:
std::deque<std::function<void()>> mTaskQueue; std::deque<std::function<void()>> mTaskQueue;
std::mutex mTaskQueueMutex; std::mutex mTaskQueueMutex;
std::condition_variable mWorkerCondition; std::condition_variable mWorkerCondition;
std::atomic<size_t> mNumTasksInSystem;
std::mutex mSystemBusyMutex;
std::condition_variable mSystemBusyCondition;
}; };
NGP_NAMESPACE_END NGP_NAMESPACE_END
...@@ -28,7 +28,6 @@ ThreadPool::ThreadPool(size_t maxNumThreads, bool force) { ...@@ -28,7 +28,6 @@ ThreadPool::ThreadPool(size_t maxNumThreads, bool force) {
maxNumThreads = min((size_t)thread::hardware_concurrency(), maxNumThreads); maxNumThreads = min((size_t)thread::hardware_concurrency(), maxNumThreads);
} }
startThreads(maxNumThreads); startThreads(maxNumThreads);
mNumTasksInSystem.store(0);
} }
ThreadPool::~ThreadPool() { ThreadPool::~ThreadPool() {
...@@ -59,16 +58,6 @@ void ThreadPool::startThreads(size_t num) { ...@@ -59,16 +58,6 @@ void ThreadPool::startThreads(size_t num) {
lock.unlock(); lock.unlock();
task(); task();
mNumTasksInSystem--;
{
unique_lock<mutex> localLock{mSystemBusyMutex};
if (mNumTasksInSystem == 0) {
mSystemBusyCondition.notify_all();
}
}
} }
}); });
} }
...@@ -90,30 +79,16 @@ void ThreadPool::shutdownThreads(size_t num) { ...@@ -90,30 +79,16 @@ void ThreadPool::shutdownThreads(size_t num) {
} }
} }
void ThreadPool::waitUntilFinished() { void ThreadPool::setNThreads(size_t num) {
unique_lock<mutex> lock{mSystemBusyMutex}; if (mNumThreads > num) {
shutdownThreads(mNumThreads - num);
if (mNumTasksInSystem == 0) { } else if (mNumThreads < num) {
return; startThreads(num - mNumThreads);
}
mSystemBusyCondition.wait(lock);
}
void ThreadPool::waitUntilFinishedFor(const chrono::microseconds Duration) {
unique_lock<mutex> lock{mSystemBusyMutex};
if (mNumTasksInSystem == 0) {
return;
} }
mSystemBusyCondition.wait_for(lock, Duration);
} }
void ThreadPool::flushQueue() { void ThreadPool::flushQueue() {
lock_guard<mutex> lock{mTaskQueueMutex}; lock_guard<mutex> lock{mTaskQueueMutex};
mNumTasksInSystem -= mTaskQueue.size();
mTaskQueue.clear(); mTaskQueue.clear();
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment