I'm new to multithread programming in C++, and am trying to use thread pools in my code. My code is pretty simple.
#include <iostream>
#include <vector>
#include <thread>
const int SIZE = 100000;
void foo() {
std::cout << "foo" << std::endl;
}
int main() {
std::vector<std::thread> myThreads;
for (int i = 0; i < SIZE; i++) {
myThreads.push_back(std::thread(foo));
}
for (auto& myThread : myThreads) {
myThread.join();
}
return 0;
}
When I run this code from Visual Studio 15 on Windows 10, no problem. It works. My issue is when I run it on my Raspberry Pi 3 I get an error that says:
terminate called after throwing an instance of 'std::system_error'
what(): Resource temporarily unavailable
Now I assume that what's happening is that the Pi's weak CPU simply can't handle such a large quantity of threads at once. When I change the SIZE down to 100 or 200, the code executes fine.
So why is it that this many threads causes the program to fail? Do the threads not wait to be executed or what is the problem?