From b647f01d2de521d6e68ed39accdbe3b144b05406 Mon Sep 17 00:00:00 2001 From: Twarit Waikar Date: Tue, 1 Mar 2022 17:02:48 +0530 Subject: [PATCH] threadpool: Guard exceptions array behind a mutex --- p4-fusion/thread_pool.cc | 4 ++++ p4-fusion/thread_pool.h | 1 + 2 files changed, 5 insertions(+) diff --git a/p4-fusion/thread_pool.cc b/p4-fusion/thread_pool.cc index 3bf6afde..aff12aa0 100644 --- a/p4-fusion/thread_pool.cc +++ b/p4-fusion/thread_pool.cc @@ -42,6 +42,8 @@ void ThreadPool::Wait() void ThreadPool::RaiseCaughtExceptions() { + std::unique_lock lock(m_ThreadExceptionsMutex); + for (auto& exceptionPtr : m_ThreadExceptions) { if (exceptionPtr) @@ -123,6 +125,8 @@ void ThreadPool::Initialize(int size) } catch (const std::exception& e) { + std::unique_lock lock(m_ThreadExceptionsMutex); + m_ThreadExceptions[i] = std::current_exception(); } m_JobsProcessing--; diff --git a/p4-fusion/thread_pool.h b/p4-fusion/thread_pool.h index 1f2f679f..73a430f2 100644 --- a/p4-fusion/thread_pool.h +++ b/p4-fusion/thread_pool.h @@ -21,6 +21,7 @@ class ThreadPool typedef std::function Job; std::vector m_Threads; + std::mutex m_ThreadExceptionsMutex; std::vector m_ThreadExceptions; std::vector m_ThreadNames; std::vector m_P4Contexts;