public member function
<mutex>

std::timed_mutex::try_lock_until

template <class Clock, class Duration>  bool try_lock_until (const chrono::time_point<Clock,Duration>& abs_time);
Try to lock until time point
Attempts to lock the timed_mutex, blocking until abs_time at most:
  • If the timed_mutex isn't currently locked by any thread, the calling thread locks it (from this point, and until its member unlock is called, the thread owns the timed_mutex).
  • If the timed_mutex is currently locked by another thread, execution of the calling thread is blocked until unlocked or until abs_time, whichever happens first (meanwhile, other non-locked threads continue their execution).
  • If the timed_mutex is currently locked by the same thread calling this function, it produces a deadlock (with undefined behavior). See recursive_timed_mutex for a timed mutex type that allows multiple locks from the same thread.

All lock and unlock operations on the timed_mutex follow a single total order, with all visible effects synchronized between the lock operations and previous unlock operations on the same object.

Parameters

abs_time
A point in time at which the thread will stop blocking, abandoning the attempt to lock.
time_point is an object that represents a specific absolute time.

Return value

true if the function succeeds in locking the timed_mutex for the thread.
false otherwise.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// timed_mutex::try_lock_until example
#include <iostream>       // std::cout
#include <chrono>         // std::chrono::system_clock
#include <thread>         // std::thread
#include <mutex>          // std::timed_mutex
#include <ctime>          // std::time_t, std::tm, std::localtime, std::mktime

std::timed_mutex cinderella;

// gets time_point for next midnight:
std::chrono::time_point<std::chrono::system_clock> midnight() {
  using std::chrono::system_clock;
  std::time_t tt = system_clock::to_time_t (system_clock::now());
  struct std::tm * ptm = std::localtime(&tt);
  ++ptm->tm_mday; ptm->tm_hour=0; ptm->tm_min=0; ptm->tm_sec=0;
  return system_clock::from_time_t (mktime(ptm));
}

void carriage() {
  if (cinderella.try_lock_until(midnight())) {
    std::cout << "ride back home on carriage\n";
    cinderella.unlock();
  }
  else
    std::cout << "carriage reverts to pumpkin\n";
}

void ball() {
  cinderella.lock();
  std::cout << "at the ball...\n";
  cinderella.unlock();
}

int main ()
{
  std::thread th1 (ball);
  std::thread th2 (carriage);

  th1.join();
  th2.join();

  return 0;
}

Possible output (order of lines may be the opposite, or carriage reverted to pumpkin):
at the ball...
ride back home on carriage


Data races

The timed_mutex object is accessed/modified as an atomic operation (causes no data races).

Exception safety

If the timed_mutex is currently locked by the calling thread, it causes undefined behavior.
Otherwise, it offers the same level of guarantee as the operations on the duration object (for the types used by the clocks in <chrono>, this is a no-throw guarantee).

See also