将向量传递给c ++中的线程函数

时间:2015-09-04 19:15:26

标签: c++ multithreading function vector

我正在尝试运行多线程矩阵求和函数,以便每行在不同的线程中汇总。我已经尝试实现了将vector传递给模板类中的cpp线程函数的所有变通方法,但我仍然遇到这个常见错误。

代码:

template <typename T> class Matrix
{
    // variables for matrix size and variables in a one dimension vector
    unsigned int _rows;
    unsigned int _cols;
    vector<vector<T> > _matrix;

    // Matrix template class functions declarations (all common operators and constructors)

    void sumLine(vector<T>& first, vector<T>& second, vector<T>& result);
    Matrix<T> operator+(const Matrix<T> & other) const;
};

 // Matrix template class functions implmentations

template <typename T> void Matrix<T>::sumLine(vector<T>& first, vector<T>& second, vector<T>& result)
{
    for (unsigned int colIdx = 0; colIdx < _cols; colIdx++)
    {
        result[colIdx] = first[colIdx] + second[colIdx];
    }
}

template <typename T> Matrix<T> Matrix<T>::operator+(const Matrix<T> & other) const
{
    vector<thread> threads;
    vector<vector<T> > results;
    vector<T> newRow(_cols);
    results.resize(_rows, newRow);
    for (unsigned int rowIdx = 0; rowIdx < _rows; rowIdx++)
    {
        vector<T> first = _matrix[rowIdx];
        vector<T> second = other._matrix[rowIdx];
        vector<T> result = results[rowIdx];
        threads.push_back(thread(Matrix<T>::sumLine, std::ref(first), std::ref(second), std::ref(result)));
    }
    for (unsigned int thrdIdx = 0; thrdIdx < _rows; thrdIdx++)
    {
        threads[thrdIdx].join();
    }

    // do something with vector<vector<T>> results
}

仍然在用gcc编译之后我得到了:

    In file included from /usr/lib/gcc/x86_64-pc-cygwin/4.9.3/include/c++/thread:39:0,
                 from Matrix.hpp:12,
                 from main.cpp:13:
/usr/lib/gcc/x86_64-pc-cygwin/4.9.3/include/c++/functional: In instantiation of 'struct std::_Bind_simple<std::_Mem_fn<void (Matrix<Complex>::*)(std::vector<Complex, std::allocator<Complex> >&, std::vector<Complex, std::allocator<Complex> >&, std::vector<Complex, std::allocator<Complex> >&)>(std::reference_wrapper<std::vector<Complex, std::allocator<Complex> > >, std::reference_wrapper<std::vector<Complex, std::allocator<Complex> > >, std::reference_wrapper<std::vector<Complex, std::allocator<Complex> > >)>':
/usr/lib/gcc/x86_64-pc-cygwin/4.9.3/include/c++/thread:137:47:   required from 'std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (Matrix<Complex>::*)(std::vector<Complex, std::allocator<Complex> >&, std::vector<Complex, std::allocator<Complex> >&, std::vector<Complex, std::allocator<Complex> >&); _Args = {std::reference_wrapper<std::vector<Complex, std::allocator<Complex> > >, std::reference_wrapper<std::vector<Complex, std::allocator<Complex> > >, std::reference_wrapper<std::vector<Complex, std::allocator<Complex> > >}]'
Matrix.hpp:404:102:   required from 'Matrix<T> Matrix<T>::operator+(const Matrix<T>&) const [with T = Complex]'
main.cpp:59:14:   required from here
/usr/lib/gcc/x86_64-pc-cygwin/4.9.3/include/c++/functional:1665:61: error: no type named 'type' in 'class std::result_of<std::_Mem_fn<void (Matrix<Complex>::*)(std::vector<Complex, std::allocator<Complex> >&, std::vector<Complex, std::allocator<Complex> >&, std::vector<Complex, std::allocator<Complex> >&)>(std::reference_wrapper<std::vector<Complex, std::allocator<Complex> > >, std::reference_wrapper<std::vector<Complex, std::allocator<Complex> > >, std::reference_wrapper<std::vector<Complex, std::allocator<Complex> > >)>'
       typedef typename result_of<_Callable(_Args...)>::type result_type;
                                                             ^
/usr/lib/gcc/x86_64-pc-cygwin/4.9.3/include/c++/functional:1695:9: error: no type named 'type' in 'class std::result_of<std::_Mem_fn<void (Matrix<Complex>::*)(std::vector<Complex, std::allocator<Complex> >&, std::vector<Complex, std::allocator<Complex> >&, std::vector<Complex, std::allocator<Complex> >&)>(std::reference_wrapper<std::vector<Complex, std::allocator<Complex> > >, std::reference_wrapper<std::vector<Complex, std::allocator<Complex> > >, std::reference_wrapper<std::vector<Complex, std::allocator<Complex> > >)>'
         _M_invoke(_Index_tuple<_Indices...>)

当Complex是我为矩阵模板类编写的复数类时,我的主函数试图用它来计算。这有什么不对?如果它在复杂类中,是否有更简单的方法将参数传递给线程函数以避免这种情况?

2 个答案:

答案 0 :(得分:0)

你应该发布你的Complex类,这可能是问题所在。

也在你的代码中: vector<T> first = _matrix[rowIdx]; vector<T> second = other._matrix[rowIdx]; vector<T> result = results[rowIdx];

你应该这样写:vector<T>& first = _matrix[rowIdx];

因为您正在复制这些向量...如果您不修改它们,甚至是const vector<T>& first = _matrix[rowIdx];

然后你可以删除std :: ref

答案 1 :(得分:0)

以下代码编译(使用clang 3.5)并避免将对temporaries的引用传递给任何<add name="PC_ProductsProgramsPage" ...

thread