Skip to content

Instantly share code, notes, and snippets.

@tiandiao123
Created December 21, 2022 21:36
Show Gist options
  • Save tiandiao123/db7458c5e9a316318d1a0a9120168f68 to your computer and use it in GitHub Desktop.
Save tiandiao123/db7458c5e9a316318d1a0a9120168f68 to your computer and use it in GitHub Desktop.
#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