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
const_lock.h File Reference

Scope-based const-locking utility for mutex-like synchronization primitives. More...

Go to the source code of this file.

Classes

class  jh::sync::const_lock< Mutex >
 Scope-based immutability barrier for mutex_like types. More...

Namespaces

namespace  jh::sync
 Aggregated entry point for synchronization and coordination facilities.

Detailed Description

Scope-based const-locking utility for mutex-like synchronization primitives.

Author
JeongHan-Bae <mastropseudo@gmail.com>

This header defines jh::sync::const_lock, a lightweight RAII-style synchronization helper that enforces immutability barriers for shared or exclusive access. It provides a unified locking abstraction compatible with all types satisfying jh::concepts::mutex_like.

When constructed, const_lock automatically acquires the appropriate lock:

  • If the mutex type satisfies shared_lockable, it uses lock_shared().
  • Otherwise, it falls back to exclusive lock().

Upon destruction, it symmetrically releases the held lock.

If the supplied mutex is jh::typed::null_mutex_t, all operations are completely no-op, producing zero runtime overhead. This enables seamless single-threaded or thread-local use cases without conditional compilation.

Typical usage:

std::shared_mutex sm;
jh::sync::const_lock guard(sm); // Acquires shared lock automatically
std::mutex m;
jh::sync::const_lock exclusive_guard(m); // Acquires exclusive lock
Scope-based immutability barrier for mutex_like types.
Definition const_lock.h:99
null_mutex_t null_mutex
Global constant instance of null_mutex_t.
Definition null_mutex.h:159
Version
1.3.x
Date
2025