-
Future object is returned by the async function.
Promise is returned by async.
include <iostream>
#include <future>
#include <thread>
int main() {
// future from an async()
std::future <int> f2 = std::async(std::launch::async,
[]{ //Creating lambda
return 8;
});
f2.wait();
std::cout << "Done!\nResults are:" << f2.get();
}
$ g++ test.cpp -lpthread
$ ./a.out
Done 8
// another Example
#include <iostream>
#include <thread>
#include <iostream>
#include <future>
void fun(){
std::cout << "Hello";
}
int main() {
auto fut = std::async (std::launch::async, fun);
}
$ ./a.out
Hello