|
JH-Toolkit v1.4.1
An engineering-oriented C++20 toolkit with duck-typed concepts, static design, async coroutines, and semantic containers — header-only, RTTI-free, and concurrency-friendly.
|
Strong ordering adapters for read-write mutex-like synchronization primitives. More...
#include <shared_mutex>#include <atomic>#include <jh/conceptual/mutex_like.h>#include "jh/macros/platform.h"Go to the source code of this file.
Classes | |
| class | jh::sync::strong_unique_lock< Mutex > |
| Strong exclusive lock adapter. More... | |
| class | jh::sync::strong_shared_lock< Mutex > |
| Strong shared (reader) lock adapter. More... | |
Namespaces | |
| namespace | jh::sync |
| Aggregated entry point for synchronization and coordination facilities. | |
Typedefs | |
| template<jh::concepts::basic_lockable Mtx> | |
| using | jh::sync::posix_smtx_unique_lock = std::unique_lock<Mtx> |
| POSIX native exclusive lock alias. | |
| template<jh::concepts::shared_lockable Mtx> | |
| using | jh::sync::posix_smtx_shared_lock = std::shared_lock<Mtx> |
| POSIX native shared lock alias. | |
Strong ordering adapters for read-write mutex-like synchronization primitives.
On POSIX platforms (e.g., Linux or Darwin), std::shared_mutex is commonly implemented on top of pthread_rwlock or futex-based primitives. In practice:
std::unique_lock<std::shared_mutex> behaves similarly to pthread_rwlock_wrlock() / pthread_rwlock_unlock(). std::shared_lock<std::shared_mutex> behaves similarly to pthread_rwlock_rdlock() / pthread_rwlock_unlock(). However, ISO C++ does not strictly guarantee identical global ordering semantics compared to POSIX implementations. In particular, acquire-release semantics alone may be insufficient when locks and atomics are mixed under high concurrency.
Even when atomics use memory_order_seq_cst, observable reordering may still occur under combinations of: high concurrency, debug builds, and intrusive test frameworks. The strong variants aim to reduce such cross-domain ordering anomalies.
This header introduces two strengthening adapters:
strong_unique_lock requires jh::concepts::basic_lockable. strong_shared_lock requires jh::concepts::shared_lockable. strong_unique_lock and strong_shared_lockstd::atomic_thread_fence(std::memory_order_seq_cst) immediately after acquisition and immediately before release. posix_smtx_*_lockstd::thread handles. Such issues originate from thread management semantics, not from std::shared_mutex itself.1.4.1
2026