[英]How to join an array of threads in C++?
我正在學習如何在 C++ 中使用線程。 我需要創建一系列威脅,開始執行所有威脅,然后加入它們。 但是,我收到錯誤消息,有人可以幫助我嗎?
我的代碼框架是:
#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();
}
}
}
}
我得到了例外:
Unhandled exception at 0x76BF40B2 in Learn.exe: Microsoft C++ exception: std::system_error at memory location 0x1DF3F4E0.
我知道這個 問題,但是添加if (Threads[i][j].joinable())
沒有幫助。 非常感謝您的幫助!
Learn.exe 中 0x76BF40B2 處的未處理異常:Microsoft C++ 異常:內存位置 0x1DF3F4E0 處的 std::system_error。
這主要是因為您創建了數量驚人的線程
thread Threads[10000][1000];
大多數系統無法支持該數量的線程。 這是 Linux 系統支持的典型最大線程數:
~$ cat /proc/sys/kernel/threads-max
124898
Windows 可以不同,但很可能不在數百萬的范圍內。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.