【发布时间】:2016-04-26 18:22:15
【问题描述】:
我在我的 C++11 代码中运行多个线程,线程主体是使用 lambda 函数定义的,如下所示。
// make connection to each device in a separate child thread
std::vector<std::thread> workers;
for(int ii = 0; ii < numDev; ii++)
{
workers.push_back(std::thread([=]() { // pass by value
// thread body
}));
}
// detach from all threads
std::for_each(workers.begin(), workers.end(), [](std::thread &t) {
t.detach();
});
// killing one of the threads here?
我与所有子线程分离,但在工作向量中保留每个子线程的引用。以后如何在我的代码中杀死其中一个线程?
在here 中发帖建议使用std::terminate(),但我想这对我来说没有用。
【问题讨论】:
-
如果你不合作地杀死一个线程,你怎么能保证你的程序呢?
标签: multithreading c++11