Documentation Home >> Headers >> <mutex> Header >> std::unique_lock >> std::unique_lock::try_lock_for Member Function

Attempts to acquire a lock on the mutex associated with *this within the time specified.

template<typename Rep, typename Period>
bool try_lock_for(
    std::chrono::duration<Rep,Period> const&
    relative_time);

Preconditions:

The Mutex type used to instantiate std::unique_lock must meet the TimedLockable requirements. this->mutex()!=NULL, this->owns_lock()==false.

Effects:

Calls this->mutex()->try_lock_for(relative_time).

Returns:

true if the call to this->mutex()->try_lock_for() returned true, false otherwise.

Throws:

Any exceptions thrown by this->mutex()->try_lock_for(). std::system_error with an error code of std::errc::operation_not_permitted if this->mutex()==NULL. std::system_error with an error code of std::errc::resource_deadlock_would_occur if this->owns_lock()==true on entry.

Postcondition:

If the function returns true, this->owns_lock()==true, otherwise this->owns_lock()==false.

Header

#include <mutex>

See Also