From aeecf717fee464ac2b9700e571d19c0c51794d7b Mon Sep 17 00:00:00 2001 From: Dan Engelbrecht Date: Mon, 4 Dec 2023 05:16:22 -0500 Subject: safe threadpool shutdown (#584) * shut down thread pools earlier to worker threads has a chance to terminate before main thread atexit --- src/zenutil/workerpools.cpp | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'src/zenutil/workerpools.cpp') diff --git a/src/zenutil/workerpools.cpp b/src/zenutil/workerpools.cpp index b511b0c5c..3ae302064 100644 --- a/src/zenutil/workerpools.cpp +++ b/src/zenutil/workerpools.cpp @@ -14,6 +14,8 @@ namespace { const int LargeWorkerThreadPoolTreadCount = gsl::narrow(std::thread::hardware_concurrency()); const int SmallWorkerThreadPoolTreadCount = gsl::narrow(Max((std::thread::hardware_concurrency() / 4u), 1u)); + static bool IsShutDown = false; + RwLock PoolLock; std::unique_ptr LargeWorkerPool; @@ -32,6 +34,7 @@ GetLargeWorkerPool() } } RwLock::ExclusiveLockScope _(PoolLock); + ZEN_ASSERT(!IsShutDown); if (LargeWorkerPool) { return *LargeWorkerPool; @@ -51,6 +54,7 @@ GetSmallWorkerPool() } } RwLock::ExclusiveLockScope _(PoolLock); + ZEN_ASSERT(!IsShutDown); if (SmallWorkerPool) { return *SmallWorkerPool; @@ -70,6 +74,7 @@ GetSyncWorkerPool() } } RwLock::ExclusiveLockScope _(PoolLock); + ZEN_ASSERT(!IsShutDown); if (SyncWorkerPool) { return *SyncWorkerPool; @@ -82,6 +87,7 @@ void ShutdownWorkerPools() { RwLock::ExclusiveLockScope _(PoolLock); + IsShutDown = true; LargeWorkerPool.reset(); SmallWorkerPool.reset(); SyncWorkerPool.reset(); -- cgit v1.2.3