From 1343caaee6eb5696ac955ad55f11c2715133fbae Mon Sep 17 00:00:00 2001 From: PowerUser64 Date: Tue, 19 Nov 2024 15:08:22 -0800 Subject: [PATCH] mutex(fix): fixed race condition (part 2) In addition to the previous commit, this commit makes threads wait for the thread manager to say it took the mutex from them before they can ask for it again. --- main.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/main.cpp b/main.cpp index ca4be50..49cea53 100644 --- a/main.cpp +++ b/main.cpp @@ -56,9 +56,10 @@ void *thread_task_increment(const struct thread_data &thread) { { // tell the thread manager we want the mutex + while (!*thread.manager_took_mutex) (*thread.wants_mutex) = true; // block until we have the mutex - while (!*thread.has_mutex && *thread.manager_took_mutex) + while (!*thread.has_mutex) ; // enter the mutex