如何声明线程的向量
how to declare a vector of thread
我是c++编程新手,需要一些帮助来使用带有矢量库的线程库…
首先我遵循这个教程
但是编译器(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++在不同线程中改变向量
- C++:在多个线程中访问同一数组/向量的不同单元格是否会产生数据竞赛?
- 在线程函数中处理数据向量时进行线程竞速
- std::vector::clear()ing 二维向量线程的内部向量安全吗?
- 在不同的线程中调用向量析构函数或清除
- 为同一向量C++运行多个线程时出现分段错误
- 具有多线程支持的 RenderClass,将函数调用推送到向量以在另一个线程上调用
- C++中向量和列表的非写入成员函数的线程安全性
- 在向量上声明缩减,在 1 个线程上运行给出的结果与没有 openmp 的结果不同
- 为什么向量"emplace_back"在多个线程中的行为比单个线程慢得多
- 由 std::shared_ptr 向量指向的数据之间的线程同步
- 在 Valgrind 输出中仍然可以访问 std 线程向量的块
- 在为工作线程访问 lambda 中捕获的向量列表中的元素引用时,是否需要互斥锁?
- C++重用调用同一函数的线程向量
- 在 C++11 中计算字母和单词的双字母组合的 std::线程向量的问题
- 在循环中创建线程向量的问题
- 提升多线程向量的返回值
- 尝试用函数填充线程向量 - 错误
- 多线程向量