1 #include <posix/timer.h>
2 
3 #include <iostream>
4 #include <mutex>
5 #include <thread>
6 
7 using namespace std;
8 
9 static std::timed_mutex mutex_test;
10 
task0_entry(void)11 static void task0_entry(void)
12 {
13     bool                 islock;
14     chrono::milliseconds waittime(2000);
15 
16     auto start  = chrono::steady_clock::now();
17     islock = mutex_test.try_lock_for(waittime);
18     auto end    = std::chrono::steady_clock::now();
19 
20     chrono::duration<double, ratio<1, 1000>> diff = end - start;
21 
22     cout << "thread0 trylock result:" << islock << " time to try lock "
23          << diff.count() << " ms" << endl;
24 }
25 
task1_entry(void)26 static void task1_entry(void)
27 {
28     bool                 islock;
29     chrono::milliseconds waittime(1000);
30     auto                 start = chrono::steady_clock::now();
31     islock                     = mutex_test.try_lock_for(waittime);
32     auto                 end   = chrono::steady_clock::now();
33     chrono::duration<float, ratio<1, 1000>> diff = end - start;
34     cout << "thread1 trylock result:" << islock << " time to try lock "
35          << diff.count() << " ms" << endl;
36 }
37 
timed_mutex_test(void)38 void timed_mutex_test(void)
39 {
40     cout << "timed mutex test start" << endl;
41     thread thread0(task0_entry);
42     thread thread1(task1_entry);
43     thread0.join();
44     thread1.join();
45     cout << "timed mutex test ok" << endl;
46 }
47