I have a problem when running the following in Visual studio 2015.
#include <thread>
#include <vector>
#include <iostream>
using namespace std;
int main() {
for (int i = 0; i < 10000; i++) {
cout << "Loop: " << i << endl;
vector<thread> threads;
for (int j = 0; j < 8; j++) {
try {
threads.push_back(thread([]() {int a = 4; a++; }));
}
catch (const system_error &se) {
cout << se.what() << endl;
exit(1);
}
}
for (int j = 0; j < 8; j++) {
threads.at(j).join();
}
}
return 0;
}
After a few thousand loops have been run the program catches a system_error with the output:
...
3994
3995
3996
3997
3998
resource unavailable try again: resource unavailable try again
I know that there is a maximum number of threads that can be run simultaneously but in this case there's only 8 being run simultaneously after which they're destroyed. Is there a maximum on the total number that can be created?
I have tried running the above on Linux with g++ and it runs fine its only on windows (both visual studio and mingw) that the error occurs. Any help? Thanks.
push_backcopies, you probably want to try again withemplace_back