C++ 没有匹配的构造函数用于初始化';标准:螺纹';
我一直在研究一个相当简单的工具:一个并发的C++ 没有匹配的构造函数用于初始化';标准:螺纹';,c++,multithreading,c++14,C++,Multithreading,C++14,我一直在研究一个相当简单的工具:一个并发的for循环构造,它包含一个输入元素列表、一个输出向量和一个从输入元素中计算输出元素的函数 我有一个未编译的代码段: template<class In, class Out> void thread_do(net::coderodde::concurrent::queue<In>& input_queue, Out (*p
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));
}
然而,我不知道如何修复它。试图将
&
预先添加到线程
/追加
,但无效 这个最小的完整示例(提示)向您展示了如何在另一个线程中调用模板成员函数
#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();
}
#包括
结构X
{
模板作废运行(A、B)
{
}
模板
无效运行(A、B)
{
mythread=std::thread(&X::run,this,a,b);
}
线程读取;
};
int main()
{
X;
x、 与(10,12)一起运行;
x、 mythread.join();
}
请注意,
std::thread
的构造函数无法自动推断模板参数。您必须是明确的。您需要实例化您的函数:
thread_vector.push_back(std::thread(thread_do<In, Out>, // you need to instantiate your template function
std::ref(input_queue), // pass parameters by ref
std::ref(process), // - // -
std::ref(output_vector))// - // -
);
thread\u vector.push\u back(std::thread(thread\u do,//您需要实例化模板函数
std::ref(输入队列),//按ref传递参数
标准::参考(工艺),//-//-
标准::ref(输出向量))/-//-
);
thread\u do既是模板函数又是类方法。std::thread的构造函数需要反映这一点。哇,太多的代码了。到目前为止,您本月一直在调试的线程在哪里?“但是,我不知道如何修复它。尝试在线程\u do/追加之前添加,但没有效果。”您可以尝试阅读文档,“这真的应该是你的第一步,而不是疯狂猜测。@Lightness Race Sinorbit下一步你建议阅读整个标准吗?@coderodde:想想你在阅读过程中会学到的所有其他有趣的东西。”。查看文档达数天是一件好事。
thread_vector.push_back(std::thread(thread_do<In, Out>, // you need to instantiate your template function
std::ref(input_queue), // pass parameters by ref
std::ref(process), // - // -
std::ref(output_vector))// - // -
);