-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathFuture_01.cpp
More file actions
75 lines (49 loc) · 1.78 KB
/
Future_01.cpp
File metadata and controls
75 lines (49 loc) · 1.78 KB
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
70
71
72
73
74
75
// ===========================================================================
// Future_01.cpp // Promises and Futures
// ===========================================================================
#include "../Logger/Logger.h"
#include <future>
#include <iostream>
#include <thread>
namespace PromisesAndFutures01 {
using namespace std::chrono_literals;
static void doWorkVersion01(std::promise<int>* promise)
{
Logger::log(std::cout, "Inside Thread - 01");
std::this_thread::sleep_for(3s);
promise->set_value(123);
}
static void testVersion01() {
std::promise<int> promiseObj{};
std::future<int> futureObj{ promiseObj.get_future() };
std::thread t {doWorkVersion01, &promiseObj };
Logger::log(std::cout, "Waiting for Result - 01");
int result{ futureObj.get() };
Logger::log(std::cout, "Result: ", result);
t.detach();
}
static void doWorkVersion02(std::promise<int>&& promise)
{
Logger::log(std::cout, "Inside Thread - 02");
std::this_thread::sleep_for(5s);
promise.set_value(123);
}
static void testVersion02() {
std::promise<int> promiseObj{};
std::future<int> futureObj{ promiseObj.get_future() };
std::thread t{ doWorkVersion02, std::move (promiseObj) };
Logger::log(std::cout, "Waiting for Result - 02");
int result{ futureObj.get() };
Logger::log(std::cout, "Result: ", result);
t.detach();
}
}
void test_future_promise_01()
{
using namespace PromisesAndFutures01;
testVersion01();
testVersion02();
}
// ===========================================================================
// End-of-File
// ===========================================================================