Should not a compiler call the destructor of the future future right after the main finishes, that is, should not be the function f() called anyway? (gcc 4.7.2 doesn’t do that).
#include <iostream>
#include <thread>
#include <future>
using namespace std;
void f() {
cout << "thread...\n";
}
int main() {
auto future = async(&f);
cout << "I am main\n";
}
edit: I get only Hello from main. The text thread... is NOT printed at all.
edit 2: Does the destructor of a future call wait()??
Attention: outdated information (pre-C++14) below. See Jonas’ answer for up-to-date information.
Right before
mainfinishes. But yes.No, why? What makes you think that the destructor of
std::futurewould do that? This isn’t the destructor’s job. In fact, according to §30.6.6/9, the only function of the destructor is to release the shared state of the future and destroy*this. Nothing more.