【发布时间】:2018-11-15 23:00:31
【问题描述】:
我正在构建一个程序,出于测试目的,它可以在 C++ 中创建 N 个线程。 我是 C++ 的相对论新手,到目前为止我目前的尝试是
//Create a list of threads
std::vector<std::thread> t;
for(i=0; i < THREADS; i ++){
std::thread th = std::thread([](){ workThreadProcess(); });
t.push_back(th);
printf("Thread started \n");
}
for(std::thread th : t){
th.join();
}
我目前的一个错误是调用“std::thread”的已删除构造函数。我不知道这意味着什么或如何解决
注意:
我看过:
- Create variable number of std::threads
- Variable number of threads c++
- Array of threads and attempting to pass multiple arguments to function is not working?
- vector of std::threads
- Creating N number of threads
但我觉得他们没有回答我的问题。他们中的大多数使用 pthread 或不同的构造函数。
【问题讨论】:
-
当询问构建错误时,首先尝试创建一个Minimal, Complete, and Verifiable Example,您可以向我们展示。然后将该示例中的完整构建日志复制粘贴(作为文本)到问题正文中。并请在示例中标出错误所在的行,例如 cmets。另外请花一些时间查看how to ask good questions,以及this question checklist。
-
一个提示:你循环加入线程可能不应该复制线程对象。
-
错误信息没有说谎。 std::thread 定义为 'thread(const thread&) = delete;'你不能复制它。如果是您,您将如何为线程编写复制构造函数?你能找出代码中的副本并以某种方式修复它吗?
-
提示:线程不可复制。
标签: c++ multithreading loops