8#include <condition_variable>
20 template<
typename Mutex>
37 template<
typename Rep,
typename Period>
40 if constexpr (std::is_same_v<Mutex, std::timed_mutex> ||
41 std::is_same_v<Mutex, std::recursive_timed_mutex>) {
105 template<
typename Lock>
115 template<
typename Lock,
typename Predicate>
116 void wait(Lock& lock, Predicate predicate) {
117 cv_.wait(lock, predicate);
126 template<
typename Lock,
typename Rep,
typename Period>
127 bool wait_for(Lock& lock,
const std::chrono::duration<Rep, Period>& timeout) {
128 return cv_.wait_for(lock, timeout) == std::cv_status::no_timeout;
138 template<
typename Lock,
typename Rep,
typename Period,
typename Predicate>
139 bool wait_for(Lock& lock,
const std::chrono::duration<Rep, Period>& timeout, Predicate predicate) {
140 return cv_.wait_for(lock, timeout, predicate);
158 std::condition_variable
cv_;
180 bool test_and_set(std::memory_order order = std::memory_order_acq_rel)
noexcept {
181 return flag_.test_and_set(order);
188 void clear(std::memory_order order = std::memory_order_release)
noexcept {
197 bool test(std::memory_order order = std::memory_order_acquire)
const noexcept {
198 return flag_.test(order);
205 void wait(
bool expected, std::memory_order order = std::memory_order_acquire)
const noexcept {
206 flag_.wait(expected, order);
275 return mutex_.try_lock_shared();
Enhanced atomic flag with additional operations.
atomic_flag_wrapper & operator=(atomic_flag_wrapper &&)=default
atomic_flag_wrapper(atomic_flag_wrapper &&)=default
~atomic_flag_wrapper()=default
void notify_one() noexcept
Notify one thread waiting on this flag.
void wait(bool expected, std::memory_order order=std::memory_order_acquire) const noexcept
Wait until the flag becomes false.
bool test(std::memory_order order=std::memory_order_acquire) const noexcept
Test the flag without modifying it.
void clear(std::memory_order order=std::memory_order_release) noexcept
Clear the flag atomically.
atomic_flag_wrapper(const atomic_flag_wrapper &)=delete
bool test_and_set(std::memory_order order=std::memory_order_acq_rel) noexcept
Test and set the flag atomically.
atomic_flag_wrapper & operator=(const atomic_flag_wrapper &)=delete
void notify_all() noexcept
Notify all threads waiting on this flag.
atomic_flag_wrapper() noexcept
Enhanced condition variable wrapper with timeout and predicate support.
bool wait_for(Lock &lock, const std::chrono::duration< Rep, Period > &timeout)
Wait with timeout.
condition_variable_wrapper(const condition_variable_wrapper &)=delete
void wait(Lock &lock)
Wait indefinitely for notification.
void notify_one() noexcept
Notify one waiting thread.
~condition_variable_wrapper()=default
void wait(Lock &lock, Predicate predicate)
Wait with predicate until condition is met.
bool wait_for(Lock &lock, const std::chrono::duration< Rep, Period > &timeout, Predicate predicate)
Wait with timeout and predicate.
condition_variable_wrapper()=default
std::condition_variable cv_
condition_variable_wrapper(condition_variable_wrapper &&)=default
void notify_all() noexcept
Notify all waiting threads.
condition_variable_wrapper & operator=(const condition_variable_wrapper &)=delete
condition_variable_wrapper & operator=(condition_variable_wrapper &&)=default
RAII-based scoped lock guard with timeout support.
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.
scoped_lock_guard(scoped_lock_guard &&)=delete
scoped_lock_guard(const scoped_lock_guard &)=delete
scoped_lock_guard & operator=(const scoped_lock_guard &)=delete
~scoped_lock_guard()
Destructor - automatically releases the lock if held.
scoped_lock_guard(Mutex &mutex, const std::chrono::duration< Rep, Period > &timeout)
Construct and try to acquire the lock with timeout.
scoped_lock_guard & operator=(scoped_lock_guard &&)=delete
Shared mutex wrapper with reader-writer lock semantics.
shared_mutex_wrapper()=default
void lock()
Acquire exclusive (writer) lock.
~shared_mutex_wrapper()=default
bool try_lock_shared()
Try to acquire shared (reader) lock.
void unlock()
Release exclusive (writer) lock.
void lock_shared()
Acquire shared (reader) lock.
shared_mutex_wrapper(const shared_mutex_wrapper &)=delete
shared_mutex_wrapper & operator=(shared_mutex_wrapper &&)=delete
shared_mutex_wrapper & operator=(const shared_mutex_wrapper &)=delete
shared_mutex_wrapper(shared_mutex_wrapper &&)=delete
bool try_lock()
Try to acquire exclusive (writer) lock.
void unlock_shared()
Release shared (reader) lock.
std::shared_lock< shared_mutex_wrapper > shared_lock
std::unique_lock< std::mutex > unique_lock