skip to Main Content

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


  1. Creating a thread is expensive. Maybe you should try ThreadPool:

    Thread pooling in C++11

    https://www.geeksforgeeks.org/thread-pool-in-cpp/

    Login or Signup to reply.
  2. What is the limit to the number of threads that can be spawned – and can it be increased?

    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.

    But this code throws a std::system_error at memory location… when commenting in 16 "folders"

    Most likely it is related to data race or buffer overrun. Try address sanitizer (ASAN).

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search