dispenso 1.6.0
A library for task parallelism
Loading...
Searching...
No Matches
dispenso::DistributedRWLock< N > Class Template Reference

#include <distributed_rw_lock.h>

Public Member Functions

void lock_shared ()
 
void unlock_shared ()
 
bool try_lock_shared ()
 
void lock ()
 
bool try_lock ()
 
void unlock ()
 

Detailed Description

template<size_t N = 16>
class dispenso::DistributedRWLock< N >

A distributed reader/writer lock compatible with std::shared_mutex.

Uses dispenso::threadId() to pick a sub-lock slot. threadId() is a fast thread-local counter (constant-initialized, lazy-filled on first call) that monotonically distributes threads as 0, 1, 2, ... — masked by N this gives uniform distribution across sub-locks with a single TEB read per call. The lazy-init pattern matters on MSVC, where a static thread_local T x = ... with a non-trivial initializer would generate a per-call TLS init guard.

Template Parameters
NNumber of sub-locks. Must be a power of 2. Higher N = better read scalability under high thread counts, but O(N) write cost. Default 16 balances read distribution vs. write overhead.
Note
Readers and the writer-bit acquisition pure-spin (no OS backoff); only a writer's reader-drain phase blocks in the OS (futex/WaitOnAddress/condvar) to avoid burning CPU while it waits for active readers to finish. Best suited for guarding fast operations with high read traffic and very rare writes.

Definition at line 89 of file distributed_rw_lock.h.

Member Function Documentation

◆ lock()

template<size_t N = 16>
void dispenso::DistributedRWLock< N >::lock ( )
inline

Acquire exclusive (write) access.

Definition at line 108 of file distributed_rw_lock.h.

◆ lock_shared()

template<size_t N = 16>
void dispenso::DistributedRWLock< N >::lock_shared ( )
inline

Acquire shared (read) access.

Definition at line 92 of file distributed_rw_lock.h.

◆ try_lock()

template<size_t N = 16>
bool dispenso::DistributedRWLock< N >::try_lock ( )
inline

Try to acquire exclusive (write) access.

Returns
true if acquired, false otherwise.

Definition at line 114 of file distributed_rw_lock.h.

◆ try_lock_shared()

template<size_t N = 16>
bool dispenso::DistributedRWLock< N >::try_lock_shared ( )
inline

Try to acquire shared (read) access.

Returns
true if acquired, false if a writer holds the lock.

Definition at line 103 of file distributed_rw_lock.h.

◆ unlock()

template<size_t N = 16>
void dispenso::DistributedRWLock< N >::unlock ( )
inline

Release exclusive (write) access.

Definition at line 119 of file distributed_rw_lock.h.

◆ unlock_shared()

template<size_t N = 16>
void dispenso::DistributedRWLock< N >::unlock_shared ( )
inline

Release shared (read) access.

Definition at line 97 of file distributed_rw_lock.h.


The documentation for this class was generated from the following file: