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.)
2
Answers
Creating a thread is expensive. Maybe you should try ThreadPool:
Thread pooling in C++11
https://www.geeksforgeeks.org/thread-pool-in-cpp/
There are limits, as every thread consumes system resources (OS handles, memory for stack). I’m 99% sure C++ does not define the limits. It is rather platform specific. I remember the days when I experimented with threads and I created thousands of them.
See:
Also take into account that the more threads you have, the more job is for the OS task scheduler.
Most likely it is related to data race or buffer overrun. Try address sanitizer (ASAN).