C++11线程:将向量传递给线程函数时出错
C++11 Threads: Error passing a vector to a thread function
作为一个更大项目的一部分,我正在开发一个多线程的中值函数。我几乎没有C++经验。下面的中值函数应该取一个3维int向量的向量,并返回一个3维int向量,其中每个条目是输入向量中该索引中所有条目的中值。因此,如果输入是<lt;3,2,1>,<1,2,3>,<2,2,2>>,返回<2,2,2>。此代码将用于实现用于实时视频的中值模糊,因此希望对其进行多线程处理
#include <thread>
#include <iostream>
#include <mutex>
#include <vector>
#include <algorithm>
#include "median.h"
// mutex to protect bgrPixel (possibly not needed)
std::mutex mtx;
std::vector<int> median(const std::vector<std::vector<int> >& input)
{
std::vector<int> bgrPixel; // Vector to store median BGR value
std::thread first(thread_function, bgrPixel, input, 0); // thread for each colour channel
std::thread second(thread_function, bgrPixel, input, 1);
std::thread third(thread_function, bgrPixel, input, 2);
first.join();
second.join();
third.join();
return bgrPixel;
}
void thread_function(std::vector<int>& bgrPixel, const std::vector<std::vector<int> >& input1, int channel)
{
std::vector<int> input = input1[channel]; // copy the colour channel
std::sort(input.begin(), input.end());
int size = input.size();
if (size %2 == 0) // get the median
{
mtx.lock();
bgrPixel[channel] = (input[size/2] + input[size/2 + 1])/2;
mtx.unlock();
} else
{
mtx.lock();
bgrPixel[channel] = input[(size-1)/2];
mtx.unlock();
}
}
我遇到的问题是,在编译时,g++(还有clang)给出了一个相当难以理解的错误:
g++ -std=c++11 -pthread -o median median.cpp
In file included from /usr/include/c++/4.8.2/thread:39:0,
from median.cpp:1:
/usr/include/c++/4.8.2/functional: In instantiation of ‘struct std::_Bind_simple<void (*(std::vector<int>, std::vector<std::vector<int> >, int))(std::vector<int>&, const std::vector<std::vector<int> >&, int)>’:
/usr/include/c++/4.8.2/thread:137:47: required from ‘std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (&)(std::vector<int>&, const std::vector<std::vector<int> >&, int); _Args = {std::vector<int, std::allocator<int> >&, const std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > >&, int}]’
median.cpp:15:58: required from here
/usr/include/c++/4.8.2/functional:1697:61: error: no type named ‘type’ in ‘class std::result_of<void (*(std::vector<int>, std::vector<std::vector<int> >, int)) (std::vector<int>&, const std::vector<std::vector<int> >&, int)>’
typedef typename result_of<_Callable(_Args...)>::type result_type;
^
/usr/include/c++/4.8.2/functional:1727:9: error: no type named ‘type’ in ‘class std::result_of<void (*(std::vector<int>, std::vector<std::vector<int> >, int)) (std::vector<int>&, const std::vector<std::vector<int> >&, int)>’
_M_invoke(_Index_tuple<_Indices...>)
^
我发现了一个类似的错误消息c++11Thread类如何使用类成员函数,但它并没有专门处理我的问题。任何帮助都将不胜感激,我完全希望这是因为我不知道我在做什么:P
EDIT:thread_function和median的原型包含在头文件median.h中。
更换
std::thread first(thread_function, bgrPixel, input, 0);
通过
std::thread first(thread_function, std::ref(bgrPixel), std::ref(input), 0);
现场示例:http://coliru.stacked-crooked.com/a/630775aafc3d4642
相关文章:
- C++使用params创建线程函数会导致转换错误
- 如何在大函数中编写多线程函数?
- C++11:将矢量元素作为线程传递到线程函数中
- 线程函数中的c++参数更改
- 我需要线程函数在不停止实际程序的情况下,每2秒打印一次随机数
- 在多线程函数中返回共享的常量指针会导致计时问题吗?
- 将项目插入 CTreeCtrl 时线程函数的奇怪行为
- C++ 线程函数通过引用传递
- 线程函数参数的前向声明不起作用
- 线程函数无法从堆上分配的缓冲区中读取字符
- 线程函数不打印
- C :如何在复杂模板类中指定线程函数
- 为什么我在C++线程函数调用中得到重复值?
- C++ 线程函数指针实现返回错误无效使用非静态成员函数
- 线程函数中的通用引用
- 如何同时启动两个具有不同线程函数的线程团队
- std::c++ 中的线程在设置线程函数时不会出错
- 对抽象类的引用不能传递给线程函数?
- 如何将ifstream作为参数传递给std ::线程函数
- 如何线程函数