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!