After experiencing crashes when introducing nested calls of std::async in my real program, I was able to reproduce the problem in the following minimum example. It crashes often, but not always. Do you see anything what goes wrong, or is it a compiler or standard library bug? Note that the problem remains if get()
calls to the futures are added.
#include <future>
#include <vector>
int main (int, char *[])
{
std::vector<std::future<void>> v;
v.reserve(100);
for (int i = 0; i != 100; ++i)
{
v.emplace_back(std::async(std::launch::async, [] () {
std::async(std::launch::async, [] { });
}));
}
return 0;
}
I observe two different kinds of crashes: (in about every fifth run)
- Termination with "This application has requested the Runtime to terminate it in an unusual way."
- Termination after throwing an instance of 'std::future_error', what(): Promise already satisfied.
Environment:
- Windows 7
- gcc version 4.8.2 (i686-posix-dwarf-rev3, Built by MinGW-W64 project), as provided by Qt 5.3.2
- Command line call:
g++ -std=c++11 -pthread futures.cpp
- Compiled and run on two independent machines
Option -pthread
?
Could it be that in my environment for some reason the option -pthread
is silently not taken into account? I observe the same behavior with and without that option.