-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfuture_test.cpp
More file actions
35 lines (26 loc) · 899 Bytes
/
Copy pathfuture_test.cpp
File metadata and controls
35 lines (26 loc) · 899 Bytes
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
#include <iostream>
#include <folly/futures/Promise.h>
#include <fmt/core.h>
int main() {
folly::Promise<int> prom;
folly::Future<int> fut = prom.getFuture();
std::thread t([p = std::move(prom)]() mutable {
std::this_thread::sleep_for(std::chrono::milliseconds(2));
p.setValue(42);
});
fmt::print("Waiting for result...(1)\n");
fmt::print("Waiting for result...(2)\n");
fmt::print("Waiting for result...(3)\n");
auto g = std::move(fut).thenValue([](int result) {
if (result) {
fmt::print("Branch taken, result was non-zero\n");
} else {
fmt::print("Branch not taken, result was zero\n");
}
fmt::print("Result: {}\n", result);
return result; // optional, just to keep g as Future<int>
});
fmt::print("Main continues without blocking.\n");
std::move(g).wait();
t.join();
}