Wait for all tasks completed in thread pool

PiperOrigin-RevId: 542866176
This commit is contained in:
Guogang Li
2023-06-23 08:30:37 -07:00
committed by Copybara-Service
parent 1e43f4d83e
commit 72af220dae
3 changed files with 90 additions and 22 deletions
@@ -23,6 +23,7 @@
#include "absl/base/thread_annotations.h"
#include "absl/synchronization/mutex.h"
#include "internal/platform/count_down_latch.h"
#include "internal/platform/runnable.h"
namespace nearby {
@@ -37,11 +38,9 @@ class ThreadPool {
// into the thread pool.
bool Run(Runnable task) ABSL_LOCKS_EXCLUDED(mutex_);
// The thread pool is closed immediately if there is no outstanding work,
// I/O, timer, or wait objects that are bound to the pool; otherwise, the
// thread pool is released asynchronously after the outstanding objects are
// freed.
void ShutDown() ABSL_LOCKS_EXCLUDED(mutex_);
// In Nearby platform, thread pool should make sure all queued tasks completed
// in shut down.
void ShutDown();
private:
ThreadPool(PTP_POOL thread_pool, TP_CALLBACK_ENVIRON thread_pool_environ,
@@ -65,6 +64,12 @@ class ThreadPool {
// The maximum thread count in the thread pool
int max_pool_size_ ABSL_GUARDED_BY(mutex_) = 0;
// Current running task count
int running_tasks_count_ ABSL_GUARDED_BY(mutex_) = 0;
// The latch is used to wait for running tasks
std::unique_ptr<CountDownLatch> shutdown_latch_ = nullptr;
friend VOID CALLBACK WorkCallback(PTP_CALLBACK_INSTANCE instance,
PVOID parameter, PTP_WORK work);
};