【发布时间】:2018-11-17 06:29:19
【问题描述】:
#include <iostream>
#include <thread>
void func() {
std::cout << "Hello";
}
int main() {
std::vector<std::thread> threads;
int n = 100;
for (int i = 0; i < n; i++) {
std::cout << "executing thread" << std::endl;
threads.push_back(std::thread(func));
}
}
我的程序打印一次“执行线程”并结束。是什么原因?
【问题讨论】:
-
您的程序可能在任何线程实际开始之前结束。但是,是的,如果您想要答案,请提供 MCVE。
-
您遇到的当前问题与副本中的相同。一旦你解决了问题,这里的所有答案都会告诉你。
-
@UKMonkey,“在函数中启动线程”是什么意思?
-
@jameslarge 查看编辑历史
标签: c++ multithreading vector