Is there any potential problem in the code snippet below if std::future::get()
would not be called?
I did several tests. It seems that the said code works well without invoking std::future::get()
even if it takes std::async
a long time to finish its work . It's really out of my expectation.
#include<future>
#include<iostream>
#include<array>
#include<algorithm>
#include<thread>
#include<vector>
std::array<int, 100000> arr;
int sum=0;
struct Wrapper
{
void consume()
{
std::cout << "consumer:" << std::this_thread::get_id() << std::endl;
std::for_each(arr.begin(), arr.end(), [](int val) {sum+=val; });
}
void produce()
{
std::cout << "producer:" <<std::this_thread::get_id() << std::endl;
int a=0;
while(true)
{
if(a++>1e9)
{
break;
}
}
}
};
int main()
{
std::fill(arr.begin(), arr.end(), 1);
std::cout << "main:" <<std::this_thread::get_id() << std::endl;
Wrapper wrap;
std::vector<std::future<void>> vec;
vec.push_back(std::async(std::launch::async, &Wrapper::produce, &wrap));
vec.push_back(std::async(std::launch::async, &Wrapper::consume, &wrap));
#ifdef WAIT //Is there any potencial problem if the block below does not run?
for(auto& future:vec)
{
future.get();
}
#endif
}
As per the document about std::future destructor (emphasis mine):
But it seems only guaranteed by
C++14
and afterwards other thanC++11
.And for some cases,
std::future::get()
still needs to be explicitly called, for example:Here is the output of the said code snippet above: