-2

I am learning how to use threads in C++. I need to create an array of threats, start executing all of the threats and subsequently join them. I am getting an error however, can someone help me please?

The skeleton of my code is:

#include <iostream>
#include <thread>
using namespace std;

int Numbers[100000][1000];
thread Threads[10000][1000];

// Function to be passed to thread
void Simulate(int i, int j) {
    Numbers[i][j] = i + j;
}

int main()
{
    // Start executing threads
    for (int i = 0; i < 10000; i++) {
        for (int j = 0; j < 1000; j++) {
            Threads[i][j] = thread(Simulate, i, j);
        }
    }
    // Wait till all of them finish
    for (int i = 0; i < 10000; i++) {
        for (int j = 0; j < 1000; j++) {
            if (Threads[i][j].joinable()) {
                Threads[i][j].join();
            }
        }
    }
}

I get the exception:

Unhandled exception at 0x76BF40B2 in Learn.exe: Microsoft C++ exception: std::system_error at memory location 0x1DF3F4E0.

I am aware of this question but the addition of if (Threads[i][j].joinable()) did not help. Thank you very much for any help!

artm
  • 17,291
  • 6
  • 38
  • 54
  • 1
    Use `std::array` or `std::vector` instead of raw arrays. – πάντα ῥεῖ Sep 03 '20 at 10:20
  • 2
    You're attempting to create 10,000,000 threads. Chances are you're simply running out of resources. Try running under a debugger to find out where the exception is being thrown. – G.M. Sep 03 '20 at 10:22
  • 3
    Why do you even want 10 Million threads? This is going to slow your program down and consume huge amounts of memory. Just for playing around? Then you just found out that you can't have unlimited threads. – Lukas-T Sep 03 '20 at 10:23
  • It would make much more sense to split your work into the number of threads which can at most run truely in parallel. For this, [std::thread::hardware_concurrency()](https://en.cppreference.com/w/cpp/thread/thread/hardware_concurrency) can be obtained. FYI: [SO: Multi-threading benchmarking issues](https://stackoverflow.com/a/52835213/7478597) Starting too many threads makes you process run slower but not faster compared to a single thread. – Scheff's Cat Sep 03 '20 at 11:55

1 Answers1

5

Unhandled exception at 0x76BF40B2 in Learn.exe: Microsoft C++ exception: std::system_error at memory location 0x1DF3F4E0.

That mostly because you created an insane number of threads

thread Threads[10000][1000];

Most systems cannot support that number of threads. This is a typical max number of threads that a Linux system support:

~$ cat /proc/sys/kernel/threads-max
124898

Windows can be different, but most likely not in the range of millions.

artm
  • 17,291
  • 6
  • 38
  • 54
  • 2
    Windows doesn't seem to have a hard cap on number of threads, but memory will be the limiting factor. 10 million windows threads = 10 million stacks a 1MB ~ 10TB of Ram. – Lukas-T Sep 03 '20 at 10:32
  • 1
    @churill Agree, just note that typically, when each thread allocates its stack, it is not fully mapped to the physical memory. There may be mapped only a single page or so. Even in such a case, 1e7 x 4kB is 38GB. – Daniel Langr Sep 03 '20 at 10:53
  • 1
    @DanielLangr Ah, my addition is now obsolete :) – eerorika Sep 03 '20 at 10:55
  • This explained it to me! Thank you so much. I played with options for stack reserve size in the Visual Studio and it fixed the problem. I originally reserved too much stack memory per single thread. – Greenhorn3.14 Sep 03 '20 at 11:02