-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpromiseDemo.cpp
More file actions
22 lines (22 loc) · 745 Bytes
/
promiseDemo.cpp
File metadata and controls
22 lines (22 loc) · 745 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// promise example
#include <iostream> // std::cout
#include <functional> // std::ref
#include <thread> // std::thread
#include <future> // std::promise, std::future
void print_int(std::future<int>& fut) {
// Thread waits here untill the promise value is set
int x = fut.get();
std::cout << "value: " << x << '\n';
}
int main()
{
std::promise<int> prom; // create promise
std::future<int> fut = prom.get_future(); // engagement with future
// Make sure to pass the fut as reference and not value
std::thread th1(print_int, std::ref(fut)); // send future to new thread
// Untill the value is set, the above thread will be waiting
prom.set_value(10); // fulfill promise
//(synchronizes with getting the future)
th1.join();
return 0;
}