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.
Loading...
Searching...
No Matches
strong_lock.h File Reference

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.

Detailed Description

Strong ordering adapters for read-write mutex-like synchronization primitives.

Author
JeongHan-Bae <mastropseudo@gmail.com>

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 and strong_shared_lock
    They enforce a global ordering barrier via std::atomic_thread_fence(std::memory_order_seq_cst) immediately after acquisition and immediately before release.
  • posix_smtx_*_lock
    On Windows, aliases the strong variants to approximate POSIX-style behavior. On POSIX systems, aliases the standard library lock types.
Note
This facility does not address lifecycle inconsistencies introduced by test frameworks that intercept or wrap std::thread handles. Such issues originate from thread management semantics, not from std::shared_mutex itself.
The primary target platform remains POSIX. The Windows strengthening path exists to provide a bounded, practical approximation for cross-platform engineering scenarios.
Version
1.4.1
Date
2026