C++ 没有用于“std::thread”初始化的匹配构造函数
我一直在研究一个相当简单的工具:一个并发for循环构造,它获取一个输入元素列表、一个输出向量和一个从输入元素中计算输出元素的函数 我有一个未编译的代码段:C++ 没有用于“std::thread”初始化的匹配构造函数,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 (*pr
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));
}
然而,我不知道如何修复它。已尝试在线程\u do/追加中预加(&U),但无效 这个最小、完整的示例提示显示了如何在另一个线程中调用模板成员函数
#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();
}
请注意,std::thread的构造函数无法自动推断模板参数。您必须是显式的。这个最小、完整的示例提示向您展示了如何在另一个线程中调用模板成员函数
#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();
}
请注意,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_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_do既是模板函数又是类方法。std::thread的构造函数需要反映这一点。哇,太多的代码了。本月到目前为止,您一直在调试的版本在哪里?但是,我不知道如何修复它。已尝试在线程\u do/追加中预加(&U),但无效。您可以尝试阅读文档,这本应该是您的第一步,而不是疯狂猜测。@LightnessRacesinOrbit下一步您是否建议阅读整个标准?@coderodde:想想您在阅读过程中会学到的所有其他有趣的东西。浏览文档几天是一件好事。thread_do既是一个模板函数,也是一个类方法。std::thread的构造函数需要反映这一点。哇,太多的代码了。本月到目前为止,您一直在调试的版本在哪里?但是,我不知道如何修复它。已尝试在线程\u do/追加中预加(&U),但无效。您可以尝试阅读文档,这本应该是您的第一步,而不是疯狂猜测。@LightnessRacesinOrbit下一步您是否建议阅读整个标准?@coderodde:想想您在阅读过程中会学到的所有其他有趣的东西。查看文档达数天是一件好事。