From 3c329bb44cb9d0feaa128bc2012b0c77529bc146 Mon Sep 17 00:00:00 2001 From: Aleksey Loginov Date: Tue, 27 Feb 2024 23:37:51 +0300 Subject: [PATCH] Fix classic race with atomics + mutex --- old_build/_deps/snitch-src | 1 + src/rpp/rpp/schedulers/new_thread.hpp | 12 ++++++++---- 2 files changed, 9 insertions(+), 4 deletions(-) create mode 160000 old_build/_deps/snitch-src diff --git a/old_build/_deps/snitch-src b/old_build/_deps/snitch-src new file mode 160000 index 000000000..2f6230890 --- /dev/null +++ b/old_build/_deps/snitch-src @@ -0,0 +1 @@ +Subproject commit 2f623089022ba09d4bcaf21772684b94a160a1e0 diff --git a/src/rpp/rpp/schedulers/new_thread.hpp b/src/rpp/rpp/schedulers/new_thread.hpp index d8bba7fa3..4aa176dec 100644 --- a/src/rpp/rpp/schedulers/new_thread.hpp +++ b/src/rpp/rpp/schedulers/new_thread.hpp @@ -45,8 +45,10 @@ namespace rpp::schedulers if (!m_thread.joinable()) return; - // just notify - m_state->is_destroying.store(true, std::memory_order::seq_cst); + { + std::lock_guard lock{m_state->mutex}; + m_state->is_destroying.store(true, std::memory_order::relaxed); + } m_state->cv.notify_all(); m_thread.detach(); } @@ -69,8 +71,10 @@ namespace rpp::schedulers if (!m_thread.joinable()) return; - // just need atomicity, not guarding anything - m_state->is_disposed.store(true, std::memory_order::seq_cst); + { + std::lock_guard lock{m_state->mutex}; + m_state->is_disposed.store(true, std::memory_order::relaxed); + } m_state->cv.notify_all(); if (m_thread.get_id() != std::this_thread::get_id())