From 14fe7047fed21fec6fadc846b42a9e804c90dbae Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 20 Nov 2026 13:53:39 +0000 Subject: [PATCH] ThreadSafe: take the lock once in the constructor After the MinGW toolchain update (GCC 16.2, new winpthreads) four test programs abort in Debug with shared_mutex:205: __shared_mutex_pthread::lock(): Assertion '__ret == 0' failed in the two tests that start several threads on a freshly constructed ThreadSafe. The locking in those tests is ordinary: ten threads each taking the exclusive lock. winpthreads initialises a read-write lock lazily on first use, and concurrent first use can return an error rather than the lock; in a Release build that error is ignored and the lock is simply not held. Take and release the lock once in the constructor, before the object can be shared, so the lock is fully set up by the time other threads reach it. This is a workaround based on that reading of the failure; CI on the MinGW jobs is the test of it. Co-Authored-By: Claude Fable 5.2 Claude-Session: https://claude.ai/code/session_011vmiZ7F3rQu7dith9sQw71 --- include/bolt/core/thread_safety.hpp | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/include/bolt/core/thread_safety.hpp b/include/bolt/core/thread_safety.hpp index 61eea187..315ee7e0 100644 --- a/include/bolt/core/thread_safety.hpp +++ b/include/bolt/core/thread_safety.hpp @@ -18,7 +18,13 @@ class ThreadSafe { T data_; public: - explicit ThreadSafe(T data = T{}) : data_(std::move(data)) {} + explicit ThreadSafe(T data = T{}) : data_(std::move(data)) { + // Take and release the lock once while only this thread can see the + // object. winpthreads (MinGW) sets a read-write lock up lazily on its + // first use, and several threads making that first use at the same + // moment can get an error back instead of the lock. + std::unique_lock lock(mutex_); + } // Read operations template