没有用于初始化'std :: thread'的匹配构造函数

cod*_*dde 0 c++ multithreading c++14

我一直在研究一个相当简单的工具:一个并发for循环结构,它接受一个输入元素列表,一个输出向量,以及一个从输入元素中计算输出元素的函数.

我有这个不编译的片段:

            template<class In, class Out>
            void thread_do(net::coderodde::concurrent::queue<In>& input_queue,
                           Out (*process)(In in),
                           std::vector<Out>& output_vector)
            {
                // Pop the queue, process, and save result.
                ...
            }


                for (unsigned i = 0; i < thread_count; ++i) 
                {
                    thread_vector.push_back(std::thread(thread_do, 
                                                        input_queue,
                                                        process,
                                                        output_vector));
                }
Run Code Online (Sandbox Code Playgroud)

我用-std=c++14.


./concurrent.h:129:45: error: no matching constructor for initialization of 'std::thread'
                    thread_vector.push_back(std::thread(thread_do, 
                                            ^           ~~~~~~~~~~

但是,我不知道如何解决它.试图先&thread_do/追加<In, Out>,但没有用.

Ric*_*ges 6

这个最小的完整示例(提示)向您展示了如何在另一个线程中调用模板成员函数.

#include <thread>

struct X
{

  template<class A, class B> void run(A a, B b)
  {
  }

  template<class A, class B>
  void run_with(A a, B b)
  {
    mythread = std::thread(&X::run<A, B>, this, a, b);
  }

  std::thread mythread;
};

int main()
{
  X x;
  x.run_with(10, 12);
  x.mythread.join();
}
Run Code Online (Sandbox Code Playgroud)

请注意,std::thread构造函数无法自动推导模板参数.你必须明确.