我是 C++ 编程新手,需要一些帮助来使用带有向量库的线程库...
首先我遵循这个tutorial https://solarianprogrammer.com/2012/02/27/cpp-11-thread-tutorial-part-2/
但编译器(Visual Studio 2013)向我显示错误,我不知道如何纠正它:
函数的第一个声明
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();
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)