I've got this test code to see, if a promise object is out of lifecycle, what happen to its future object:
#include <chrono>
#include <future>
#include <iostream>
#include <thread>
using namespace std;
void getValue(future<int>& future) {
cout << "sub process 1 \n";
try {
cout << "sub process 2 \n";
int value = future.get();
cout << "sub process 3\n";
cout << value << endl;
} catch (future_error &e) {
cerr << e.code() << '\n' << e.what() << endl;
} catch (exception e) {
cerr << e.what() << endl;
}
cout << "sub process 4\n";
}
int main() {
thread t;
{
promise<int> plocal;
future<int> flocal = plocal.get_future();
t = thread(getValue, ref(flocal));
}
t.join();
return 0;
}
I expect that in getValue
function, it either prints future::get result, or throws out runtime exception and prints exception info, because plocal
has been destructed in "main".
Acturally, this program prints:
sub process 1
sub process 2
and ends.
Aucun commentaire:
Enregistrer un commentaire