With threads I know that terminate() is called when the thread-variable leaves scope:
size_t fibrec(size_t n) {
return n<2 ? 1 : fibrec(n-2)+fibrec(n-1);
}
int main() {
std::thread th{ fibrec, 35 };
// no join here
} // ~th will call terminate().
ths destructor will call terminate() when it leaves scope.
But what about futures? Where is the thread they run going? Is it detached? How is it ended?
#include <iostream>
#include <future> // async
using namespace std;
size_t fibrec(size_t n) {
return n<2 ? 1 : fibrec(n-2)+fibrec(n-1);
}
struct Fibrec {
size_t operator()(size_t n) { return fibrec(n); }
const size_t name_;
Fibrec(size_t name) : name_(name) {}
~Fibrec() { cerr << "~"<<name_<< endl; }
};
void execit() {
auto f1 = async( Fibrec{33}, 33 );
auto f2 = async( Fibrec{34}, 34 );
// no fx.get() here !!!
}; // ~f1, ~f2, but no terminate()! Where do the threads go?
int main() {
auto f0 = async( Fibrec{35}, 35 );
execit();
cerr << "fib(35)= " << f0.get() << endl;
}
When execit() is left the futures f1 and f2 are destroyed. But their threads should still be running? The destructor of Fibrec is called, of course. But where are the threads going? The program does not crash, so I guess, the become joined? Or maybe detached? Or are they stopped or canceled? I believe that is not trivially done in C++11?
The
futureis the result of the async operation, it’s is not a thread by itself. Theasyncfunction spawns a new thread to do the computation, and when that is finished, the result is written to afutureobject.Depending on the implemented policy, you will either have to call
.get()or.wait()on the future to get the result. Invoking this will do the work on the thread on which it is called