[英]std::thread::_Invoker error while programming withthreads
EDIT: I'm trying to learn how to use threads in c++. 编辑:我正在尝试学习如何在c ++中使用线程。 I've a problem with my code, it gives me the following error: 我的代码有问题,它给了我以下错误:
no matching function for call to 'std::thread::_Invoker<std::tuple<void (matrix_product<int, 0, 0>::*)(matrix_wrap<int>&, int, const matrix_wrap<int>&, const matrix_wrap<int>&), matrix_wrap<int>, int, matrix_wrap<int>, matrix_wrap<int> > >::_M_invoke(std::thread::_Invoker<std::tuple<void (matrix_product<int, 0, 0>::*)(matrix_wrap<int>&, int, const matrix_wrap<int>&, const matrix_wrap<int>&), matrix_wrap<int>, int, matrix_wrap<int>, matrix_wrap<int> > >::_Indices)'
operator()()
This is the piece of code that gives me the error (it was working before I wrote this): 这是给我错误的一段代码(它在我写这篇文章之前就已经开始了):
void do_multiply_fico(matrix_wrap<T> result, matrix_wrap<T> lhs, matrix_wrap<T> rhs) {
// Create an array of threads
std::thread threads[lhs.get_height()];
for (int i = 0; i < lhs.get_height(); ++i) {
// Initialize each thread with the function responsible of multiplying only a part of the matrices
threads[i] = std::thread(multiply_threading, result, i, lhs, rhs);
}
for (int i = 0; i < lhs.get_height(); ++i) {
// Wait until each thead has finished
threads[i].join();
}
}
void multiply_threading(matrix_wrap<T>& result, const int thread_number, const matrix_wrap<T>& lhs, const matrix_wrap<T>& rhs){
const unsigned height = result.get_height();
const unsigned width = result.get_width();
const unsigned span = lhs.get_width();
assert(span==rhs.get_height());
for (unsigned i=0; i!=height; ++i) {
for (unsigned j = 0; j != width; ++j) {
for (unsigned k = 0; k != span; ++k) {
result(i, j) += lhs(i, k) * rhs(k, j);
}
}
}
}
thanks in advance for the help. 在此先感谢您的帮助。
It looks like you're trying to construct std::thread
from a member function. 看起来你正在尝试从成员函数构造std::thread
。 That's not going to work: you need an instance to invoke it. 这不会起作用:你需要一个实例来调用它。
You also have a more serious problem, in that reference arguments will, by default, be passed to the thread
constructor as value types. 您还有一个更严重的问题,因为默认情况下,引用参数将作为值类型传递给thread
构造函数。 You'd need to wrap them in std::ref
to get it to compile and exhibit the expected behavior. 您需要将它们包装在std::ref
以使其编译并展示预期的行为。
The easier way around all that, though, would just be to pass a lambda to std::thread
: 但是,更简单的方法就是将lambda传递给std::thread
:
threads[i] = std::thread([this, &result, i, &lhs, &rhs](){
multiply_threading(result, i, lhs, rhs);
});
That way the wrapping of arguments is done through lambda capture, rather than through the vagaries of std::thread
. 这样,参数的包装是通过lambda捕获完成的,而不是通过std::thread
的变幻莫测。 (Reference capture can under certain circumstances be unsafe, but since you're joining everything in the same function you don't need to worry about that.) Note that i
is captured by value, since you're going to change its value in later iterations and need the threads you already created to capture the value it had when you created them. (参考捕获在某些情况下可能是不安全的,但是因为你加入了同一个函数中的所有东西,所以你不需要担心。)注意i
是按值捕获的,因为你要改变它的值稍后迭代并需要您已创建的线程来捕获创建它们时的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.