From d1b45b8d3290a96dd0196ee7f874243ac3575fd7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Chmiel?= Date: Mon, 29 Apr 2024 13:59:24 +0200 Subject: [PATCH] Fix attempted to destroy locked thread pool error (#5040) --- src/V3ThreadPool.h | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/V3ThreadPool.h b/src/V3ThreadPool.h index 9c880afe0..df0077bc3 100644 --- a/src/V3ThreadPool.h +++ b/src/V3ThreadPool.h @@ -115,6 +115,7 @@ class V3ThreadPool final { // CONSTRUCTORS V3ThreadPool() = default; ~V3ThreadPool() { + m_shutdown = true; if (m_multithreadingSuspended) { // Ideally we shouldn't deal with this and just call the std::abort. However, // std::exit(0) (which invokes this destructor) is called in multiple places with @@ -140,11 +141,15 @@ class V3ThreadPool final { } bool m_mutex_locked = m_mutex.try_lock(); - // try_lock can sometimes spontaneously fail even when mutex is not locked, - // make sure this isn't the case - for (int i = 0; i < VL_LOCK_SPINS; ++i) { + using namespace std::chrono_literals; + // try to obtain lock with timeout + // we need to wait for workers to start waiting + // on condition variable to perform destruction + constexpr int spins = 1s / 1ms; + for (int i = 0; i < spins; ++i) { if (VL_LIKELY(m_mutex_locked)) break; - VL_CPU_RELAX(); + std::this_thread::sleep_for(1ms); + m_mutex_locked = m_mutex.try_lock(); } if (VL_UNCOVERABLE(!m_mutex_locked)) {