A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://cplusplus.com/timed_mutex::try_lock_until below:

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:

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 valuetrue 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
timed_mutex::lock
Lock timed mutex (public member function)
timed_mutex::try_lock
Lock timed mutex if not locked (public member function)
timed_mutex::try_lock_for
Try to lock for time span (public member function)
timed_mutex::unlock
Unlock timed mutex (public member function)
sleep_until
Sleep until time point (function)

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4