Thread System 0.3.1
High-performance C++20 thread pool with work stealing and DAG scheduling
Loading...
Searching...
No Matches
kcenon::thread::sync::scoped_lock_guard< Mutex > Class Template Reference

RAII-based scoped lock guard with timeout support. More...

#include <sync_primitives.h>

Collaboration diagram for kcenon::thread::sync::scoped_lock_guard< Mutex >:
Collaboration graph

Public Member Functions

 scoped_lock_guard (Mutex &mutex)
 Construct and immediately acquire the lock.
 
template<typename Rep , typename Period >
 scoped_lock_guard (Mutex &mutex, const std::chrono::duration< Rep, Period > &timeout)
 Construct and try to acquire the lock with timeout.
 
 ~scoped_lock_guard ()
 Destructor - automatically releases the lock if held.
 
 scoped_lock_guard (const scoped_lock_guard &)=delete
 
scoped_lock_guardoperator= (const scoped_lock_guard &)=delete
 
 scoped_lock_guard (scoped_lock_guard &&)=delete
 
scoped_lock_guardoperator= (scoped_lock_guard &&)=delete
 
bool owns_lock () const noexcept
 Check if the lock was successfully acquired.
 
void unlock ()
 Explicitly release the lock before destruction.
 
 scoped_lock_guard (Mutex &mutex)
 Construct and immediately acquire the lock.
 
template<typename Rep , typename Period >
 scoped_lock_guard (Mutex &mutex, const std::chrono::duration< Rep, Period > &timeout)
 Construct and try to acquire the lock with timeout.
 
 ~scoped_lock_guard ()
 Destructor - automatically releases the lock if held.
 
 scoped_lock_guard (const scoped_lock_guard &)=delete
 
scoped_lock_guardoperator= (const scoped_lock_guard &)=delete
 
 scoped_lock_guard (scoped_lock_guard &&)=delete
 
scoped_lock_guardoperator= (scoped_lock_guard &&)=delete
 
bool owns_lock () const noexcept
 Check if the lock was successfully acquired.
 
void unlock ()
 Explicitly release the lock before destruction.
 

Private Attributes

Mutex & mutex_
 
bool locked_
 

Detailed Description

template<typename Mutex>
class kcenon::thread::sync::scoped_lock_guard< Mutex >

RAII-based scoped lock guard with timeout support.

This class provides automatic lock management with optional timeout capabilities, ensuring locks are always released even in exceptional situations.

Definition at line 205 of file forward.h.

Constructor & Destructor Documentation

◆ scoped_lock_guard() [1/8]

template<typename Mutex >
kcenon::thread::sync::scoped_lock_guard< Mutex >::scoped_lock_guard ( Mutex & mutex)
inlineexplicit

Construct and immediately acquire the lock.

Parameters
mutexThe mutex to lock

Definition at line 27 of file sync_primitives.h.

References kcenon::thread::sync::scoped_lock_guard< Mutex >::mutex_.

◆ scoped_lock_guard() [2/8]

template<typename Mutex >
template<typename Rep , typename Period >
kcenon::thread::sync::scoped_lock_guard< Mutex >::scoped_lock_guard ( Mutex & mutex,
const std::chrono::duration< Rep, Period > & timeout )
inline

Construct and try to acquire the lock with timeout.

Parameters
mutexThe mutex to lock
timeoutMaximum time to wait for the lock

Definition at line 38 of file sync_primitives.h.

39 : mutex_(mutex), locked_(false) {
40 if constexpr (std::is_same_v<Mutex, std::timed_mutex> ||
41 std::is_same_v<Mutex, std::recursive_timed_mutex>) {
42 locked_ = mutex_.try_lock_for(timeout);
43 } else {
44 // Fallback for mutexes without timeout support
45 locked_ = mutex_.try_lock();
46 }
47 }

References kcenon::thread::sync::scoped_lock_guard< Mutex >::locked_, and kcenon::thread::sync::scoped_lock_guard< Mutex >::mutex_.

◆ ~scoped_lock_guard() [1/2]

template<typename Mutex >
kcenon::thread::sync::scoped_lock_guard< Mutex >::~scoped_lock_guard ( )
inline

Destructor - automatically releases the lock if held.

Definition at line 52 of file sync_primitives.h.

52 {
53 if (locked_) {
54 mutex_.unlock();
55 }
56 }

References kcenon::thread::sync::scoped_lock_guard< Mutex >::locked_, and kcenon::thread::sync::scoped_lock_guard< Mutex >::mutex_.

◆ scoped_lock_guard() [3/8]

template<typename Mutex >
kcenon::thread::sync::scoped_lock_guard< Mutex >::scoped_lock_guard ( const scoped_lock_guard< Mutex > & )
delete

◆ scoped_lock_guard() [4/8]

template<typename Mutex >
kcenon::thread::sync::scoped_lock_guard< Mutex >::scoped_lock_guard ( scoped_lock_guard< Mutex > && )
delete

◆ scoped_lock_guard() [5/8]

template<typename Mutex >
kcenon::thread::sync::scoped_lock_guard< Mutex >::scoped_lock_guard ( Mutex & mutex)
inlineexplicit

Construct and immediately acquire the lock.

Parameters
mutexThe mutex to lock

Definition at line 33 of file sync_primitives.h.

34 : mutex_(mutex), locked_(true) {
35 mutex_.lock();
36 }

References kcenon::thread::sync::scoped_lock_guard< Mutex >::mutex_.

◆ scoped_lock_guard() [6/8]

template<typename Mutex >
template<typename Rep , typename Period >
kcenon::thread::sync::scoped_lock_guard< Mutex >::scoped_lock_guard ( Mutex & mutex,
const std::chrono::duration< Rep, Period > & timeout )
inline

Construct and try to acquire the lock with timeout.

Parameters
mutexThe mutex to lock
timeoutMaximum time to wait for the lock

Definition at line 44 of file sync_primitives.h.

45 : mutex_(mutex), locked_(false) {
46 if constexpr (std::is_same_v<Mutex, std::timed_mutex> ||
47 std::is_same_v<Mutex, std::recursive_timed_mutex>) {
48 locked_ = mutex_.try_lock_for(timeout);
49 } else {
50 // Fallback for mutexes without timeout support
51 locked_ = mutex_.try_lock();
52 }
53 }

References kcenon::thread::sync::scoped_lock_guard< Mutex >::locked_, and kcenon::thread::sync::scoped_lock_guard< Mutex >::mutex_.

◆ ~scoped_lock_guard() [2/2]

template<typename Mutex >
kcenon::thread::sync::scoped_lock_guard< Mutex >::~scoped_lock_guard ( )
inline

Destructor - automatically releases the lock if held.

Definition at line 58 of file sync_primitives.h.

58 {
59 if (locked_) {
60 mutex_.unlock();
61 }
62 }

References kcenon::thread::sync::scoped_lock_guard< Mutex >::locked_, and kcenon::thread::sync::scoped_lock_guard< Mutex >::mutex_.

◆ scoped_lock_guard() [7/8]

template<typename Mutex >
kcenon::thread::sync::scoped_lock_guard< Mutex >::scoped_lock_guard ( const scoped_lock_guard< Mutex > & )
delete

◆ scoped_lock_guard() [8/8]

template<typename Mutex >
kcenon::thread::sync::scoped_lock_guard< Mutex >::scoped_lock_guard ( scoped_lock_guard< Mutex > && )
delete

Member Function Documentation

◆ operator=() [1/4]

template<typename Mutex >
scoped_lock_guard & kcenon::thread::sync::scoped_lock_guard< Mutex >::operator= ( const scoped_lock_guard< Mutex > & )
delete

◆ operator=() [2/4]

template<typename Mutex >
scoped_lock_guard & kcenon::thread::sync::scoped_lock_guard< Mutex >::operator= ( const scoped_lock_guard< Mutex > & )
delete

◆ operator=() [3/4]

template<typename Mutex >
scoped_lock_guard & kcenon::thread::sync::scoped_lock_guard< Mutex >::operator= ( scoped_lock_guard< Mutex > && )
delete

◆ operator=() [4/4]

template<typename Mutex >
scoped_lock_guard & kcenon::thread::sync::scoped_lock_guard< Mutex >::operator= ( scoped_lock_guard< Mutex > && )
delete

◆ owns_lock() [1/2]

template<typename Mutex >
bool kcenon::thread::sync::scoped_lock_guard< Mutex >::owns_lock ( ) const
inlinenodiscardnoexcept

Check if the lock was successfully acquired.

Returns
true if lock is held, false otherwise

Definition at line 68 of file sync_primitives.h.

68 {
69 return locked_;
70 }

References kcenon::thread::sync::scoped_lock_guard< Mutex >::locked_.

◆ owns_lock() [2/2]

template<typename Mutex >
bool kcenon::thread::sync::scoped_lock_guard< Mutex >::owns_lock ( ) const
inlinenodiscardnoexcept

Check if the lock was successfully acquired.

Returns
true if lock is held, false otherwise

Definition at line 74 of file sync_primitives.h.

74 {
75 return locked_;
76 }

References kcenon::thread::sync::scoped_lock_guard< Mutex >::locked_.

◆ unlock() [1/2]

template<typename Mutex >
void kcenon::thread::sync::scoped_lock_guard< Mutex >::unlock ( )
inline

Explicitly release the lock before destruction.

Definition at line 75 of file sync_primitives.h.

75 {
76 if (locked_) {
77 mutex_.unlock();
78 locked_ = false;
79 }
80 }

References kcenon::thread::sync::scoped_lock_guard< Mutex >::locked_, and kcenon::thread::sync::scoped_lock_guard< Mutex >::mutex_.

◆ unlock() [2/2]

template<typename Mutex >
void kcenon::thread::sync::scoped_lock_guard< Mutex >::unlock ( )
inline

Explicitly release the lock before destruction.

Definition at line 81 of file sync_primitives.h.

81 {
82 if (locked_) {
83 mutex_.unlock();
84 locked_ = false;
85 }
86 }

References kcenon::thread::sync::scoped_lock_guard< Mutex >::locked_, and kcenon::thread::sync::scoped_lock_guard< Mutex >::mutex_.

Member Data Documentation

◆ locked_

◆ mutex_


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