我一直在研究一个相当简单的设施:并发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));
}
I use -std=c++14
.
./concurrent.h:129:45: error: no matching constructor for initialization of 'std::thread'
thread_vector.push_back(std::thread(thread_do,
^ ~~~~~~~~~~
但是,我不知道如何解决它。尝试前置&
to the thread_do
/追加<In, Out>
,但仍无济于事。
这个最小的、完整的示例(提示)向您展示了如何在另一个线程中调用模板成员函数。
#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
的构造函数无法自动推导模板参数。你必须明确。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)