C++11:将矢量元素作为线程传递到线程函数中
C++11: Passing vector elements as threads into thread functions
有没有办法将向量的每个元素作为线程传递到函数中?我尝试了以下方法并注释掉了错误。 程序应该接受一行变量(例如 1 2 3 4 5 6 7(,并将每个变量作为线程传递到线程函数中。
我将非常感谢有关此的任何帮助!
int main()
{
cout<<"[Main] Please input a list of gene seeds: "<<endl;
int value;
string line;
getline(cin, line);
istringstream iss(line);
while(iss >> value){
inputs.push_back(value);
}
for (int unsigned i = 0; i < inputs.size(); i++) {
//thread inputs.at(i)(threadFunction);
}
听起来你只是想为每个数字生成一个线程:
#include <thread>
void thread_function(int x)
{
std::cout<<"Passed Number = "<<x<<std::endl;
}
int main()
{
std::vector<std::thread> threads;
...
for (auto i = 0; i < inputs.size(); i++) {
std::thread thread_obj(thread_function, inputs.at(i));
threads.emplace_back(thread_obj);
}
...
for (auto& thread_obj : threads)
thread_obj.join();
return 0;
}
相关文章:
- C++使用params创建线程函数会导致转换错误
- 如何在大函数中编写多线程函数?
- C++11:将矢量元素作为线程传递到线程函数中
- 线程函数中的c++参数更改
- 我需要线程函数在不停止实际程序的情况下,每2秒打印一次随机数
- 在多线程函数中返回共享的常量指针会导致计时问题吗?
- 将项目插入 CTreeCtrl 时线程函数的奇怪行为
- C++ 线程函数通过引用传递
- 线程函数参数的前向声明不起作用
- 线程函数无法从堆上分配的缓冲区中读取字符
- 线程函数不打印
- C :如何在复杂模板类中指定线程函数
- 为什么我在C++线程函数调用中得到重复值?
- C++ 线程函数指针实现返回错误无效使用非静态成员函数
- 线程函数中的通用引用
- 如何同时启动两个具有不同线程函数的线程团队
- std::c++ 中的线程在设置线程函数时不会出错
- 对抽象类的引用不能传递给线程函数?
- 如何将ifstream作为参数传递给std ::线程函数
- 如何线程函数