-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathtime_constrain.cpp
69 lines (53 loc) · 1.18 KB
/
time_constrain.cpp
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <iostream>
#include <functional>
#include <thread>
#include <mutex>
#include <future>
#include <chrono>
int factorial(int n)
{
int res;
res = 1;
for (int i = 1; i <= n; i++)
res *= i;
std::cout << "Result is: " << res << std::endl;
return (res);
}
int main()
{
/*
Threads
*/
std::thread t1(factorial, 6);
std::this_thread::sleep_for(std::chrono::seconds(3));
std::chrono::steady_clock::time_point tp = std::chrono::steady_clock::now() + std::chrono::seconds(5);
std::this_thread::sleep_until(tp);
t1.join();
/*
Mutex
*/
std::mutex mu;
std::lock_guard<std::mutex> locker1(mu);
std::unique_lock<std::mutex> ulocker(mu);
ulocker.try_lock();
ulocker.try_lock_for(std::chrono::seconds(10));
ulocker.try_lock_until(tp);
/*
Condition variable
*/
std::condition_variable cond;
cond.wait_for(ulocker, std::chrono::milliseconds(10));
cond.wait_until(ulocker, tp);
/*
Future and Promise
*/
std::promise<int> prom;
std::future<int> fut;
fut = prom.get_future();
fut.get();
fut.wait();
fut.wait_for(std::chrono::seconds(2));
fut.wait_until(tp);
std::cin.get();
return 0;
}