こんにちは私はC++ &スレッドを勉強しています。私はC++を初めて使いました。次のコードは他の言語の経験に基づいています。しかし私にはそれは大丈夫だと思いますが、実行するとコンパイルされますが、実行すると何もしません。私が間違っていることを教えてください。実行中のいくつかのスレッドを取得しようとしてC++の単純なスレッドの例
#include <iostream>
#include <thread>
#include <vector>
#include <string>
void printLine(std::string str) {
std::cout << str << std::endl;
}
void child(int id) {
printLine("This is a thread with id: " + std::to_string(id));
}
int main() {
printLine("This is the main thread and we are baout to spawn threads...");
std::vector<std::thread> threads;
for (int i = 0; i < 10; i++) {
threads[i] = std::thread(child, i);
threads[i].join();
}
printLine("Press any key to exit...");
std::getchar();
return 0;
}
を境界アクセスのうちので、未定義の動作を: 'スレッド[i]を=のstd ::スレッド(子、I) ; '。ベクトル 'threads'は空です。 – juanchopanza
forループの外側でスレッドを使用していないときに、なぜスレッドをスレッドに「追加」していますか? – Default
ダウン投票している人にとって、これは馬鹿馬鹿しい質問だと思っていますが、ダウンして投票するのではなく、少なくとも私のエラーに関する参考情報を提供できれば幸いです。 – Syd