C++11:将向量元素作为线程传递到线程函数中
有没有办法将向量的每个元素作为线程传递到函数中?我尝试了以下方法并注释掉了错误。 程序应该接收一行变量SE.G12 3 4 5 6 7,并将每个变量作为线程传递给线程函数 我将非常感谢任何有关这方面的帮助C++11:将向量元素作为线程传递到线程函数中,c++,multithreading,c++11,vector,C++,Multithreading,C++11,Vector,有没有办法将向量的每个元素作为线程传递到函数中?我尝试了以下方法并注释掉了错误。 程序应该接收一行变量SE.G12 3 4 5 6 7,并将每个变量作为线程传递给线程函数 我将非常感谢任何有关这方面的帮助 int main() { cout<<"[Main] Please input a list of gene seeds: "<<endl; int value; string line; getline(cin, l
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;
}
听起来您只是想为每个数字生成一个线程:
#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;
}
将数字作为线程传递完全没有意义。数字不是线程。线程是代码和数据块。它们不是数字。将数字作为线程传递完全没有意义。数字不是线程。线程是代码和数据块。它们不是数字。如果你加入同一个循环迭代,那么你就扼杀了并行性。您需要一个启动循环和一个等待循环。@是的,修复了。如果您加入同一个循环迭代,那么您将终止并行性。您需要一个启动循环和一个等待循环。@Phil1970是的,修复了。