Template Class ReentrantSpinLock¶
Defined in File reentrant_spin_lock.h
Class Documentation¶
-
template<typename T = uint32_t>
class ReentrantSpinLock¶ Reentrant spin lock allowing the same thread to lock multiple times.
Unlike SpinLock, the same thread can acquire the lock multiple times without deadlocking. Each lock() must be matched with an unlock(). Maintains a reference count per thread.
Use when: Recursive calls may re-acquire the same lock (call chains, nested operations) Use SpinLock when: No recursion needed (simpler, slightly faster)
Example (Recursive tree traversal):
ReentrantSpinLock tree_lock; void process_node(Node* node) { std::lock_guard lock(tree_lock); // Safe even if already locked process(node); if (node->left) process_node(node->left); // Recursive call re-locks if (node->right) process_node(node->right); }
See also
SpinLock for non-reentrant version (faster when recursion not needed)
- Template Parameters:
T – Reference count type (default uint32_t). Use smaller types (uint16_t, uint8_t) if maximum recursion depth is known and small, to save memory.
Public Functions
-
inline ReentrantSpinLock()¶
-
inline bool try_lock()¶
Attempts to acquire the lock for the current thread without blocking.
- Returns:
trueif the lock was acquired,falseotherwise.
-
inline void lock()¶
Blocks until the lock can be acquired for the current thread.
-
inline void unlock()¶
Releases the non-shared lock held by the thread.
The calling thread must have previously acquired the lock.