std::timed_mutex::try_lock_until

From cppreference.com
template< class Clock, class Duration >
void 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. May block for longer than until timeout_time has been reached.

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

[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)