如何声明线程向量
how to declare a vector of thread
我是 C++ 编程的新手,需要一些帮助才能使用带矢量库的线程库...
首先我按照这个tutorial
但编译器 (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();
}
我终于可以通过更改代码来解决问题...
workers.emplace_back(thread{ [&]() {
calcIterThread(ref(res), inicio, fin, i);
}});
我是 C++ 编程的新手,需要一些帮助才能使用带矢量库的线程库...
首先我按照这个tutorial
但编译器 (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();
}
我终于可以通过更改代码来解决问题...
workers.emplace_back(thread{ [&]() {
calcIterThread(ref(res), inicio, fin, i);
}});