From 797d6d3fe0c5b14feb41d6352654254368ba889c Mon Sep 17 00:00:00 2001 From: Alexander Schneider Date: Fri, 13 Dec 2024 14:22:00 -0800 Subject: [PATCH] add isEmpty() method to and fix bug in Ocean::ThreadPool Reviewed By: janherling Differential Revision: D67174302 fbshipit-source-id: 1201715719507ffa5d803de656c82f05ec353d4a --- impl/ocean/base/ThreadPool.cpp | 2 +- impl/ocean/base/ThreadPool.h | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/impl/ocean/base/ThreadPool.cpp b/impl/ocean/base/ThreadPool.cpp index 3663d8a4c..67bc6f1b1 100644 --- a/impl/ocean/base/ThreadPool.cpp +++ b/impl/ocean/base/ThreadPool.cpp @@ -175,7 +175,7 @@ void ThreadPool::threadRun() } } - while (capacity_ < busyPoolThreads_.size() + idlePoolThreads_.size() && idlePoolThreads_.empty()) + while (capacity_ < busyPoolThreads_.size() + idlePoolThreads_.size() && !idlePoolThreads_.empty()) { // we have an idle pool thread and we have more threads than the specified capacity (e.g., because the capacity has been reduced lately) diff --git a/impl/ocean/base/ThreadPool.h b/impl/ocean/base/ThreadPool.h index fb9e5307c..acab73eea 100644 --- a/impl/ocean/base/ThreadPool.h +++ b/impl/ocean/base/ThreadPool.h @@ -145,6 +145,12 @@ class OCEAN_BASE_EXPORT ThreadPool : protected Thread */ inline size_t pending() const; + /** + * Returns whether the ThreadPool has completed all jobs that have been submitted + * @return True, if the results of size() and pending() are atomically both 0 + */ + inline bool isEmpty() const; + protected: /** @@ -220,6 +226,13 @@ inline size_t ThreadPool::pending() const return pendingFunctions_.size(); } +inline bool ThreadPool::isEmpty() const +{ + const ScopedLock scopedLock(lock_); + + return (pendingFunctions_.size() + busyPoolThreads_.size()) == 0; +} + } #endif // META_OCEAN_BASE_THREAD_POOL_H