Skip to content

Commit 3a4a989

Browse files
mhaynieclaude
andcommitted
fix: Use atomic for cross-thread access in thread_pool and test
TSan detected data races: - thread_pool::m_IsShuttingDown was accessed without synchronization - coroutine_task_test value variable was modified on thread pool and read on main thread without synchronization Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 88368c8 commit 3a4a989

File tree

2 files changed

+5
-3
lines changed

2 files changed

+5
-3
lines changed

cpp/include/mh/concurrency/thread_pool.inl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@
88

99
#include <utility>
1010

11+
#include <atomic>
12+
1113
namespace mh
1214
{
1315
namespace detail::thread_pool_hpp
1416
{
1517
struct thread_data
1618
{
17-
bool m_IsShuttingDown = false;
19+
std::atomic<bool> m_IsShuttingDown = false;
1820

1921
mh::dispatcher m_Dispatcher{ false };
2022
std::vector<std::thread> m_Threads;

test/coroutine_task_test.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,9 @@ TEST_CASE("task - exceptions in discarded tasks")
150150
{
151151
mh::thread_pool tp(2);
152152

153-
int value = 0;
153+
std::atomic<int> value = 0;
154154
// Intentionally discarding the task - cast to void to suppress nodiscard warning
155-
(void)[](mh::thread_pool& tp, int& val) -> mh::task<>
155+
(void)[](mh::thread_pool& tp, std::atomic<int>& val) -> mh::task<>
156156
{
157157
co_await tp.co_add_task();
158158
co_await tp.co_delay_for(2s);

0 commit comments

Comments
 (0)