-
Notifications
You must be signed in to change notification settings - Fork 0
/
exampleAsyncClient.cpp
82 lines (68 loc) · 1.85 KB
/
exampleAsyncClient.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
70
71
72
73
74
75
76
77
78
79
80
81
82
#include "client.h"
#include "CustomClass.h"
using namespace std;
#include <future>
int main()
{
log("I am the async client.");
const auto policy{ launch::async };
const string intKey{ "MyKeyAsync" };
const string strKey{ "She" };
const string customKey{ "CustomClassAsync" };
// sends
auto intSend{ async(policy, [intKey]() {
log("Sending int");
try {
send(intKey, 101);
}
catch (char e[]) {
// manually catch errors since exceptions only propagate on .get()
log(e);
terminate();
}
log("Sent int");
}) };
auto strSend{ async(policy, [strKey]() {
log("Sending string");
try {
send(strKey, string{ "Ra" });
} catch (char e[]) {
// manually catch errors since exceptions only propagate on .get()
log(e);
terminate();
}
log("Sent string");
}) };
auto customSend{ async(policy, [customKey]() {
log("Sending custom class");
CustomClass custom{ 42, 81 };
custom.incrementA();
custom.incrementB();
try {
send(customKey, custom);
}
catch (char e[]) {
// manually catch errors since exceptions only propagate on .get()
log(e);
terminate();
}
log("Sent custom class");
}) };
// gets
auto intGet{ async(policy, [intKey, &intSend]() {
intSend.get(); // await send success
log(intKey + " -> " + to_string(get<int>(intKey)));
}) };
auto strGet{ async(policy, [strKey, &strSend]() {
strSend.get(); // await send success
log(strKey + " -> " + get<string>(strKey));
}) };
auto customGet{ async(policy, [customKey, &customSend]() {
customSend.get(); // await send success
auto savedCustom{ get<CustomClass>(customKey) };
savedCustom.incrementA();
savedCustom.incrementB();
log("A=" + to_string(savedCustom.getA()) + ", B=" + to_string(savedCustom.getB()));
}) };
getchar(); // wait before closing
}