Back to questionsWhen and in which thread does
JuniorTheoryCommon
When and in which thread does answer run in this program?
#include <future>
#include <iostream>
int answer() {
return 42;
}
int main() {
std::future<int> f = std::async(std::launch::deferred, answer);
std::cout << f.get() << '\n';
}Inside f.get(), in the thread of main, which calls get
- Expecting
std::asyncto start the function at once: withstd::launch::deferrednothing runs untilget()orwait()is called. - Expecting
get()to start a new thread: a deferred function runs in the thread that callsget(), like an ordinary function call.
- What changes if the policy is
std::launch::async? - Which policy does
std::async(answer)use when no policy is given?