Created
December 21, 2022 21:36
-
-
Save tiandiao123/db7458c5e9a316318d1a0a9120168f68 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <future> | |
#include <iostream> | |
#include <thread> | |
using namespace std; | |
int factorial(std::future<int>& f){ | |
int N = f.get(); | |
int res = 1; | |
for(int i=2;i<=N;i++){ | |
res *= i; | |
} | |
return res; | |
} | |
int main(){ | |
int x; | |
std::promise<int> p; | |
std::future<int> f = p.get_future(); | |
std::future<int> fu = std::async(factorial, std::ref(f)); | |
// do something else | |
std::this_thread::sleep_for(chrono::milliseconds(1000)); | |
p.set_value(6); | |
x = fu.get(); | |
cout <<"the result is : " << x << endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment