public member function
<mutex>
std::timed_mutex::try_lock_untiltemplate <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:true
if the function succeeds in locking the timed_mutex for the thread.
false
otherwise.
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;
}
at the ball... ride back home on carriage
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