我已經運行在Visual Studio 2015年C++創造循環線程時錯誤
#include <thread>
#include <vector>
#include <iostream>
using namespace std;
int main() {
for (int i = 0; i < 10000; i++) {
cout << "Loop: " << i << endl;
vector<thread> threads;
for (int j = 0; j < 8; j++) {
try {
threads.push_back(thread([]() {int a = 4; a++; }));
}
catch (const system_error &se) {
cout << se.what() << endl;
exit(1);
}
}
for (int j = 0; j < 8; j++) {
threads.at(j).join();
}
}
return 0;
}
以下幾千元的循環已經在運行該程序後,當一個問題捕獲與輸出SYSTEM_ERROR:
...
3994
3995
3996
3997
3998
resource unavailable try again: resource unavailable try again
我知道有最大數量的線程可以同時運行,但在這種情況下,只有8個正在同時運行,之後它們被銷燬。可以創建的總數有最大值嗎?
我試着在Linux上用g ++運行上面的代碼,它運行良好,只有在windows(包括visual studio和mingw)上纔會出現錯誤。任何幫助? 謝謝。
'push_back'副本,你可能想用'emplace_back'再試一次 – Mgetz
給它一個Mgetz,但它沒有什麼區別 –
@SamVarshavchik請不要在評論中留言。事實是C++是大多數窗口的基礎,VC++團隊在WINDIV中擁有非常感興趣的客戶。 – Mgetz