From ee8ff117128cc0b553ea05c44ddd9989ee1baab7 Mon Sep 17 00:00:00 2001 From: maelstrom Date: Thu, 28 May 2026 18:04:25 +0200 Subject: [PATCH] feat: test for mutex (make this better) --- engine/tests/src/thread.cc | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/engine/tests/src/thread.cc b/engine/tests/src/thread.cc index fe7b840..f2f8811 100644 --- a/engine/tests/src/thread.cc +++ b/engine/tests/src/thread.cc @@ -1,5 +1,8 @@ #include "fishbone/thread.h" #include +#include +#include +#include struct thread_arg { int& a; @@ -30,4 +33,27 @@ TEST_CASE("Thread") { th.join(); REQUIRE(a == 100); +} + +TEST_CASE("Mutex") { + for (int i = 0; i < 100; i++) { + int a = 0; + fishbone::Mutex m; + + fishbone::Thread::CallableFunc func = [&a, &m](auto) { + m.lock(); + for (int j = 0; j < 2; j++) { + int b = a; + usleep((60 * rand()) / RAND_MAX); + a = ++b; + } + m.unlock(); + }; + + fishbone::Thread th1(func), th2(func); + + th1.join(); + th2.join(); + REQUIRE(a == 4); + } } \ No newline at end of file