I was under the impression that I could, in principle, do multi-threading with hundreds of threads simultaneously (regardless if it makes sense). But this code throws a std::system_error at memory location
… when commenting in 16 “folders” (works still fine with 10) which corresponds to (only!) 16 threads:
#include <iostream>
#include <thread>
#include <vector>
void function_t(const std::string& folder) {
std::cout << "Processing folder: " << folder << std::endl;
}
int main(int argc, char* argv[])
{
std::vector<std::string> folders{
std::string("1"),
std::string("2"),
std::string("3"),
std::string("4"),
std::string("5"),
std::string("6"),
std::string("7"),
std::string("8"),
std::string("9"),
std::string("10"),
// std::string("11"), // Commenting in these lines throws an error.
// std::string("12"),
// std::string("13"),
// std::string("14"),
// std::string("15"),
// std::string("16")
};
std::vector<std::thread> threads{};
for (const std::string& folder : folders) {
threads.emplace_back(function_t, folder);
}
for (std::thread& thread : threads) {
thread.join();
}
return 0;
}
Why is that so – and can I work around it? I’m on Windows / Visual Studio 22.
(The point is not if it MAKES SENSE to spawn 100 threads, I only try to understand where the principle limitation comes from.)
1