tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
unique_lock< Mutex > Struct Template Reference

The struct unique_lock is a general-purpose mutex ownership wrapper allowing deferred locking, time-constrained attempts at locking, recursive locking, transfer of lock ownership, and use with condition variables. More...

#include <unique_lock.hpp>

Public Types

using mutex_type = Mutex
 

Public Member Functions

constexpr unique_lock () noexcept=default
 Constructs a unique_lock with no associated mutex.
 
constexpr unique_lock (mutex_type &m)
 Constructs a unique_lock with m as the associated mutex. Additionally: Locks the associated mutex by calling m.lock(). The behavior is undefined if the current thread already owns the mutex except when the mutex is recursive.
 
constexpr unique_lock (mutex_type &m, adopt_lock_t)
 Constructs a unique_lock with m as the associated mutex. Additionally: Assumes the calling thread already owns m.
 
template<typename Rep, typename Period>
constexpr unique_lock (mutex_type &m, chrono::duration< Rep, Period > const &relTime) noexcept
 Constructs a unique_lock with m as the associated mutex. Additionally: Tries to lock the associated mutex by calling m.try_lock_for(timeout_duration). Blocks until specified timeout_duration has elapsed or the lock is acquired, whichever comes first. May block for longer than timeout_duration.
 
template<typename Clock, typename Duration>
constexpr unique_lock (mutex_type &m, chrono::time_point< Clock, Duration > const &absTime) noexcept
 Constructs a unique_lock with m as the associated mutex. Additionally: Tries to lock the associated mutex by calling m.try_lock_until(timeout_time). Blocks until specified timeout_time has been reached or the lock is acquired, whichever comes first. May block for longer than until timeout_time has been reached.
 
constexpr unique_lock (mutex_type &m, defer_lock_t) noexcept
 Constructs a unique_lock with m as the associated mutex. Additionally: Does not lock the associated mutex.
 
constexpr unique_lock (mutex_type &m, try_to_lock_t) noexcept
 Constructs a unique_lock with m as the associated mutex. Additionally: Tries to lock the associated mutex without blocking by calling m.try_lock(). The behavior is undefined if the current thread already owns the mutex except when the mutex is recursive.
 
constexpr unique_lock (unique_lock &&u) noexcept
 Move constructor. Initializes the unique_lock with the contents of other. Leaves other with no associated mutex.
 
constexpr unique_lock (unique_lock const &)=delete
 Deleted copy constructor. unique_lock is move only.
 
constexpr ~unique_lock () noexcept
 
constexpr auto lock () noexcept(noexcept(_mutex->lock())) -> void
 Locks (i.e., takes ownership of) the associated mutex.
 
constexpr auto mutex () const noexcept -> mutex_type *
 Returns a pointer to the associated mutex, or a null pointer if there is no associated mutex.
 
constexpr operator bool () const noexcept
 Checks whether *this owns a locked mutex or not.
 
constexpr auto operator= (unique_lock &&u) noexcept -> unique_lock &
 Move assignment operator. Replaces the contents with those of other using move semantics. If prior to the call *this has an associated mutex and has acquired ownership of it, the mutex is unlocked.
 
constexpr auto operator= (unique_lock const &) -> unique_lock &=delete
 Deleted copy assignment. unique_lock is move only.
 
constexpr auto owns_lock () const noexcept -> bool
 Checks whether *this owns a locked mutex or not.
 
constexpr auto release () noexcept -> mutex_type *
 Breaks the association of the associated mutex, if any, and *this. No locks are unlocked. If the *this held ownership of the associated mutex prior to the call, the caller is now responsible to unlock the mutex.
 
constexpr auto swap (unique_lock &other) noexcept -> void
 Exchanges the internal states of the lock objects.
 
constexpr auto try_lock () noexcept(noexcept(_mutex->try_lock())) -> bool
 Tries to lock (i.e., takes ownership of) the associated mutex without blocking.
 
template<typename Rep, typename Period>
constexpr auto try_lock_for (chrono::duration< Rep, Period > const &dur) noexcept(noexcept(_mutex->try_lock_for(dur))) -> bool
 Tries to lock (i.e., takes ownership of) the associated mutex. Blocks until specified timeout_duration has elapsed or the lock is acquired, whichever comes first. On successful lock acquisition returns true, otherwise returns false. Effectively calls mutex()->try_lock_for(timeout_duration). This function may block for longer than timeout_duration due to scheduling or resource contention delays.
 
template<typename Clock, typename Duration>
constexpr auto try_lock_until (chrono::time_point< Clock, Duration > const &tp) noexcept(noexcept(_mutex->try_lock_until(tp))) -> bool
 Tries to lock (i.e., takes ownership of) the associated mutex without blocking.
 
constexpr auto unlock () -> void
 Unlocks (i.e., releases ownership of) the associated mutex and releases ownership. Silently does nothing, if there is no associated mutex or if the mutex is not locked.
 

Friends

constexpr auto swap (unique_lock &lhs, unique_lock &rhs) noexcept(noexcept(lhs.swap(rhs))) -> void
 Specializes the swap algorithm for unique_lock. Exchanges the state of lhs with that of rhs.
 

Detailed Description

template<typename Mutex>
struct etl::unique_lock< Mutex >

The struct unique_lock is a general-purpose mutex ownership wrapper allowing deferred locking, time-constrained attempts at locking, recursive locking, transfer of lock ownership, and use with condition variables.

The struct unique_lock is movable, but not copyable – it meets the requirements of MoveConstructible and MoveAssignable but not of CopyConstructible or CopyAssignable. The struct unique_lock meets the BasicLockable requirements. If Mutex meets the Lockable requirements, unique_lock also meets the Lockable requirements (ex.: can be used in lock); if Mutex meets the TimedLockable requirements, unique_lock also meets the TimedLockable requirements.

Member Typedef Documentation

◆ mutex_type

template<typename Mutex>
using mutex_type = Mutex

Constructor & Destructor Documentation

◆ unique_lock() [1/9]

template<typename Mutex>
unique_lock ( )
constexprdefaultnoexcept

Constructs a unique_lock with no associated mutex.

◆ unique_lock() [2/9]

template<typename Mutex>
unique_lock ( mutex_type & m)
inlineexplicitconstexpr

Constructs a unique_lock with m as the associated mutex. Additionally: Locks the associated mutex by calling m.lock(). The behavior is undefined if the current thread already owns the mutex except when the mutex is recursive.

◆ unique_lock() [3/9]

template<typename Mutex>
unique_lock ( mutex_type & m,
defer_lock_t  )
inlineconstexprnoexcept

Constructs a unique_lock with m as the associated mutex. Additionally: Does not lock the associated mutex.

◆ unique_lock() [4/9]

template<typename Mutex>
unique_lock ( mutex_type & m,
try_to_lock_t  )
inlineconstexprnoexcept

Constructs a unique_lock with m as the associated mutex. Additionally: Tries to lock the associated mutex without blocking by calling m.try_lock(). The behavior is undefined if the current thread already owns the mutex except when the mutex is recursive.

◆ unique_lock() [5/9]

template<typename Mutex>
unique_lock ( mutex_type & m,
adopt_lock_t  )
inlineconstexpr

Constructs a unique_lock with m as the associated mutex. Additionally: Assumes the calling thread already owns m.

◆ unique_lock() [6/9]

template<typename Mutex>
template<typename Clock, typename Duration>
unique_lock ( mutex_type & m,
chrono::time_point< Clock, Duration > const & absTime )
inlineconstexprnoexcept

Constructs a unique_lock with m as the associated mutex. Additionally: Tries to lock the associated mutex by calling m.try_lock_until(timeout_time). Blocks until specified timeout_time has been reached or the lock is acquired, whichever comes first. May block for longer than until timeout_time has been reached.

◆ unique_lock() [7/9]

template<typename Mutex>
template<typename Rep, typename Period>
unique_lock ( mutex_type & m,
chrono::duration< Rep, Period > const & relTime )
inlineconstexprnoexcept

Constructs a unique_lock with m as the associated mutex. Additionally: Tries to lock the associated mutex by calling m.try_lock_for(timeout_duration). Blocks until specified timeout_duration has elapsed or the lock is acquired, whichever comes first. May block for longer than timeout_duration.

◆ unique_lock() [8/9]

template<typename Mutex>
unique_lock ( unique_lock< Mutex > const & )
constexprdelete

Deleted copy constructor. unique_lock is move only.

◆ unique_lock() [9/9]

template<typename Mutex>
unique_lock ( unique_lock< Mutex > && u)
inlineconstexprnoexcept

Move constructor. Initializes the unique_lock with the contents of other. Leaves other with no associated mutex.

◆ ~unique_lock()

template<typename Mutex>
~unique_lock ( )
inlineconstexprnoexcept

Member Function Documentation

◆ lock()

template<typename Mutex>
auto lock ( ) -> void
inlineconstexprnoexcept

Locks (i.e., takes ownership of) the associated mutex.

◆ mutex()

template<typename Mutex>
auto mutex ( ) const -> mutex_type*
inlinenodiscardconstexprnoexcept

Returns a pointer to the associated mutex, or a null pointer if there is no associated mutex.

◆ operator bool()

template<typename Mutex>
operator bool ( ) const
inlineexplicitnodiscardconstexprnoexcept

Checks whether *this owns a locked mutex or not.

◆ operator=() [1/2]

template<typename Mutex>
auto operator= ( unique_lock< Mutex > && u) -> unique_lock&
inlineconstexprnoexcept

Move assignment operator. Replaces the contents with those of other using move semantics. If prior to the call *this has an associated mutex and has acquired ownership of it, the mutex is unlocked.

◆ operator=() [2/2]

template<typename Mutex>
auto operator= ( unique_lock< Mutex > const & ) -> unique_lock &=delete
constexprdelete

Deleted copy assignment. unique_lock is move only.

◆ owns_lock()

template<typename Mutex>
auto owns_lock ( ) const -> bool
inlinenodiscardconstexprnoexcept

Checks whether *this owns a locked mutex or not.

◆ release()

template<typename Mutex>
auto release ( ) -> mutex_type*
inlinenodiscardconstexprnoexcept

Breaks the association of the associated mutex, if any, and *this. No locks are unlocked. If the *this held ownership of the associated mutex prior to the call, the caller is now responsible to unlock the mutex.

Returns
Pointer to the associated mutex or a null pointer if there was no associated mutex.

◆ swap()

template<typename Mutex>
auto swap ( unique_lock< Mutex > & other) -> void
inlineconstexprnoexcept

Exchanges the internal states of the lock objects.

◆ try_lock()

template<typename Mutex>
auto try_lock ( ) -> bool
inlineconstexprnoexcept

Tries to lock (i.e., takes ownership of) the associated mutex without blocking.

Returns
true if the ownership of the mutex has been acquired successfully, false otherwise.

◆ try_lock_for()

template<typename Mutex>
template<typename Rep, typename Period>
auto try_lock_for ( chrono::duration< Rep, Period > const & dur) -> bool
inlineconstexprnoexcept

Tries to lock (i.e., takes ownership of) the associated mutex. Blocks until specified timeout_duration has elapsed or the lock is acquired, whichever comes first. On successful lock acquisition returns true, otherwise returns false. Effectively calls mutex()->try_lock_for(timeout_duration). This function may block for longer than timeout_duration due to scheduling or resource contention delays.

◆ try_lock_until()

template<typename Mutex>
template<typename Clock, typename Duration>
auto try_lock_until ( chrono::time_point< Clock, Duration > const & tp) -> bool
inlineconstexprnoexcept

Tries to lock (i.e., takes ownership of) the associated mutex without blocking.

◆ unlock()

template<typename Mutex>
auto unlock ( ) -> void
inlineconstexpr

Unlocks (i.e., releases ownership of) the associated mutex and releases ownership. Silently does nothing, if there is no associated mutex or if the mutex is not locked.

Friends And Related Symbol Documentation

◆ swap

template<typename Mutex>
auto swap ( unique_lock< Mutex > & lhs,
unique_lock< Mutex > & rhs ) -> void
friend

Specializes the swap algorithm for unique_lock. Exchanges the state of lhs with that of rhs.


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