multithreading - c++11线程编译错误

标签 multithreading templates c++11

我尝试理解为什么我的代码会出现编译错误。我制作了一个最小的示例,它与我的项目中的代码保持相同的行为/架构和错误。目标是对向量中的对象进行并行评估。

template < class InputIterator, class OutputIterator >
void evaluate( InputIterator first, InputIterator last, OutputIterator outfirst ) {
    while( first != last ) {
        *outfirst++ = (*first++) / 2 ; // only for the example
    }
}

template < int num_t = 4 >
struct ParallelEvaluator {

    template< typename InputContainer , typename OutputContainer >
    static void eval(InputContainer const & src, OutputContainer & dst) {
        std::vector<std::thread> threads ;
        auto bg = src.begin() ;
        auto bg_r = dst.begin() ;
        for ( int i = 0 ; i < num_t ; i++ ) {
            threads.push_back(std::thread(evaluate<typename InputContainer::iterator,typename OutputContainer::iterator>
                                          ,bg,bg+4,bg_r)) ;
            bg = bg+4 ;
            bg_r = bg_r + 4 ;
        }
        std::for_each(threads.begin(),threads.end(),std::mem_fn(&std::thread::join));
    }
};


int main()
{
    std::vector<int> t = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15} ;
    std::vector<int> v(16) ;

    ParallelEvaluator<4>::eval(t,v) ;

    return 0;
}

当我尝试编译此代码时,出现以下错误:

/usr/include/c++/4.8/functional:1697: error: no type named 'type' in 'class std::result_of<void (*(__gnu_cxx::__normal_iterator<const int*, std::vector<int> >, __gnu_cxx::__normal_iterator<const int*, std::vector<int> >, __gnu_cxx::__normal_iterator<int*, std::vector<int> >))(__gnu_cxx::__normal_iterator<int*, std::vector<int> >, __gnu_cxx::__normal_iterator<int*, std::vector<int> >, __gnu_cxx::__normal_iterator<int*, std::vector<int> >)>'

我看到在线程回调中使用引用时会发生这种错误,但这里的情况并非如此。

有人知道为什么会出现这个错误吗?我需要特定的语法来使用 template 和 co 吗?

谢谢

最佳答案

bg的类型不是std::vector<int>::iterator ,就是std::vector<int>::const_iteratorsrcconst& 。偷懒一点,让编译器用 decltype 推断出正确的类型。 。 (此外,您可以通过使用 std::array<std::thread,num_t> 而不是 std::vector<std::thread> 来避免一些内存分配。):

template< typename InputContainer , typename OutputContainer >
static void eval(InputContainer const & src, OutputContainer & dst) {
    std::array<std::thread, num_t> threads ;
    auto bg = src.begin() ;
    auto bg_r = dst.begin() ;
    for ( int i = 0 ; i < num_t ; i++ ) {
        threads[i] = std::thread(evaluate<decltype(bg),decltype(bg_r)>
                                 ,bg,bg+4,bg_r)) ;
        bg = bg+4 ;
        bg_r = bg_r + 4 ;
    }
    std::for_each(threads.begin(),threads.end(),std::mem_fn(&std::thread::join));
}

如果您不觉得 lambda 冒犯,threads[i] = std::thread([=]{ evaluate(bg,bg+4,bg_r); });用更少的语法做同样的事情。

关于multithreading - c++11线程编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24454717/

相关文章:

c# - 我们应该使用 Windows 服务还是线程池?

java - 信号量场景代码

java - 从 Socket 流读取 XML

c++ - 模板的实例化会导致二进制代码重复,编译器会阻止它吗?

c++ - 线程锁定互斥量比 std::conditional_variable::wait() 更快

Python 多线程 max_workers

java - 在 Android studio 中使用 Activity 模板时无法添加新的 Java 类吗?

javascript - 下划线模板错误: function (n){return a.调用(this,n,h)}

c++ - 从 C++ vector 中推送和检索值的意外输出

gcc - MinGW 4.8.1 C++11 线程支持