如何声明线程向量 [英] how to declare a vector of thread

查看:110
本文介绍了如何声明线程向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C ++编程的新手,需要一些帮助才能将线程库与矢量库一起使用...

i'm new in c++ programing and need some help to use a thread library with vector library...

首先,我遵循此教程

但是编译器(Visual Studio 2013)向我显示了错误,但我不知道它如何纠正:

but the compiler (visual studio 2013) show me errors and I don't know how correct it:

第一个函数声明

void Fractal::calcIterThread(vector<vector<iterc>> &matriz, int desdePos, int hastaPos, int idThread){
   ...
}    

在主循环中

vector<vector<iterc>> res;
res.resize(altoPantalla);

for (int i = 0; i < altoPantalla; i++){
   res[i].resize(anchoPantalla);
}

int numThreads = 10;
vector<thread> workers(numThreads);

for (int i = 0; i < numThreads; i++){ //here diferent try
   thread workers[i] (calcIterThread, ref(res), inicio, fin, i)); // error: expresion must have a constant value
   workers[i] = thread(calcIterThread, ref(res), inicio, fin, i)); // error: no instance of constructor "std::thread::thread" matches the argument list
}

...rest of code... 

感谢您帮助澄清

推荐答案

尝试一下:

#include <functional>
#include <thread>
#include <vector>

// ...

int numThreads = 10;
std::vector<std::thread> workers;

for (int i = 0; i != numThreads; ++i)
{
    workers.emplace_back(calcIterThread, std::ref(res), inicia, fin, i);
}

for (auto & t : workers)
{
    t.join(); 
}

这篇关于如何声明线程向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆