This question already has an answer here:
I am reading about future destructors in C++ scott Meyers book
#include <future>
#include <iostream>
int calculate_the_answer_to_LtUaE() {
_sleep(100);
std::cout << "calculate_the_answer_to_LtUaE func called in thread \n";
return 1;
}
void do_stuff() {
std::cout << "do_stuff func called in main thread \n";
}
int main()
{
std::shared_future<int> st(std::async(std::launch::async, calculate_the_answer_to_LtUaE));
do_stuff();
}
When I ran above program observed following output on VS 2012
do_stuff func called in main thread
Press any key to continue . . .
According to Scott Meyers book if all following three conditions are true
1.It referes to a shared state that was created due to a call to std::async
2. The tasks launch policy is std::launch::async
3. The future is the last future referrering to shared state
In all above conditions are true future destructor will block i.e.by calling join in future destructor until thread exists.
As we can see above program I am not seeing output "calculate_the_answer_to_LtUaE func called in thread ".
Why is this behaviour?
Aucun commentaire:
Enregistrer un commentaire