std::timed_mutex::try_lock_until

From cppreference.com

template< class Clock, class Duration >
bool try_lock_until( const std::chrono::time_point<Clock,Duration>& timeout_time );
(since C++11)

Tries to lock the mutex. Blocks until specified timeout_time has been reached or the lock is acquired, whichever comes first. On successful lock acquisition returns true, otherwise returns false.

The clock tied to timeout_time is used, which means that adjustments of the clock are taken into account. Thus, the maximum duration of the block might, but might not, be less or more than timeout_time - Clock::now() at the time of the call, depending on the direction of the adjustment. The function also may block for longer than until after timeout_time has been reached due to scheduling or resource contention delays.

The behavior is undefined if the mutex is not unlocked before being destroyed, i.e. some thread still owns it.

Contents

[edit] Parameters

timeout_time - maximum time point to block until

[edit] Return value

true if the lock was acquired successfully, otherwise false.

[edit] Exceptions

(none)

[edit] Example

This example shows a two minute block

#include <iostream>
#include <ctime>
#include <chrono>
#include <mutex>
 
int main()
{
    std::timed_mutex test;
    test.try_lock();
    auto now=std::chrono::steady_clock::now();
    test.try_lock_until(now+std::chrono::minutes(2));
    std::cout << "hello world";
 
}


[edit] See also

locks the mutex, blocks if the mutex is not available
(public member function)
tries to lock the mutex, returns if the mutex is not available
(public member function)
tries to lock the mutex, returns if the mutex has been
unavailable for the specified timeout duration
(public member function)
unlocks the mutex
(public member function)