複数のスレッドを作成して繰り返し、しばらく動かすと。
resource unavailable try again: resource unavailable try again
という例外エラーが発生します。
調べた結果、以下のサイトで質問されていることがぴったり一致したのですが、いまいち解決策がわかりません。※例としてソースをお借りしました。
https://stackoverflow.com/questions/42315723/c-error-when-creating-threads-in-loops
32bit対応でビルドしています。
joinで終了待ちして、スレッドもスコープ内なので8個のスレッド完了後、破棄されているのになぜエラーになるのか。
C++
1#include <thread> 2#include <vector> 3#include <iostream> 4using namespace std; 5 6int main() { 7 8 for (int i = 0; i < 10000; i++) { 9 cout << "Loop: " << i << endl; 10 11 vector<thread> threads; 12 for (int j = 0; j < 8; j++) { 13 try { 14 threads.push_back(thread([]() {int a = 4; a++; })); 15 } 16 catch (const system_error &se) { 17 cout << se.what() << endl; 18 exit(1); 19 } 20 } 21 22 for (int j = 0; j < 8; j++) { 23 threads.at(j).join(); 24 } 25 } 26 27 return 0; 28}

回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2022/05/19 12:38