通过引用传递数组到线程
Passing an array by reference to thread
我有一个数组需要传递给多个线程进行处理。数组大小在编译时已知。
这是直接调用函数而不是通过线程调用的基本要素的工作缩减:
#include <thread>
template <class TYPE>
void function(TYPE& data, std::size_t row, std::size_t column){
data[row-1][column-1]=2;
}
int main(){
const int column(5),row(2);
std::array<std::array<int, column>, row> arr;
function(arr, row, column);
return data[row-1][column-1];
}
代码returns2,符合预期。
如果我通过
调用函数
std::thread worker(function<std::array<std::array<int,column>,row>>, arr, row, column);
我收到以下编译器错误:
g++ Testo.cpp -o Testo -std=c++11 -lpthread
In file included from /usr/include/c++/4.8/thread:39:0,
from Testo.cpp:2:
/usr/include/c++/4.8/functional: In instantiation of ‘struct std::_Bind_simple<void (*(std::array<std::array<int, 5ul>, 4ul>, int, int))(std::array<std::array<int, 5ul>, 4ul>&, long unsigned int, long unsigned int)>’:
/usr/include/c++/4.8/thread:137:47: required from ‘std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (&)(std::array<std::array<int, 5ul>, 4ul>&, long unsigned int, long unsigned int); _Args = {std::array<std::array<int, 5ul>, 4ul>&, const int&, const int&}]’
Testo.cpp:13:82: required from here
/usr/include/c++/4.8/functional:1697:61: error: no type named ‘type’ in ‘class std::result_of<void (*(std::array<std::array<int, 5ul>, 4ul>, int, int))(std::array<std::array<int, 5ul>, 4ul>&, long unsigned int, long unsigned int)>’
typedef typename result_of<_Callable(_Args...)>::type result_type;
^
/usr/include/c++/4.8/functional:1727:9: error: no type named ‘type’ in ‘class std::result_of<void (*(std::array<std::array<int, 5ul>, 4ul>, int, int))(std::array<std::array<int, 5ul>, 4ul>&, long unsigned int, long unsigned int)>’
_M_invoke(_Index_tuple<_Indices...>)
^
我可以将模板更改为
template <class TYPE>
void function(TYPE data, std::size_t row, std::size_t column){
data[row-1][column-1]=2;
}
并且编译器不再抱怨,但是 main 然后 returns 0 因为数组不再通过引用传递。
线程调用正确模板以通过引用传递数组的适当第一个参数是什么,就像直接调用函数时一样?
通过引用传递 std::ref(arr)
您的函数需要对数组的引用,但 std::thread
存储绑定参数的衰减副本。数组到指针的衰减导致指针类型的纯右值。要修复,请通过 std::reference_wrapper
传递 arr
以保留类型:
std::thread worker(..., std::ref(arr), row, column);
您可能还想使用 lambda,这样就不必显式传递模板参数:
std::thread worker([&arr]{ return function(arr, row, column); });
我有一个数组需要传递给多个线程进行处理。数组大小在编译时已知。 这是直接调用函数而不是通过线程调用的基本要素的工作缩减:
#include <thread>
template <class TYPE>
void function(TYPE& data, std::size_t row, std::size_t column){
data[row-1][column-1]=2;
}
int main(){
const int column(5),row(2);
std::array<std::array<int, column>, row> arr;
function(arr, row, column);
return data[row-1][column-1];
}
代码returns2,符合预期。 如果我通过
调用函数 std::thread worker(function<std::array<std::array<int,column>,row>>, arr, row, column);
我收到以下编译器错误:
g++ Testo.cpp -o Testo -std=c++11 -lpthread
In file included from /usr/include/c++/4.8/thread:39:0,
from Testo.cpp:2:
/usr/include/c++/4.8/functional: In instantiation of ‘struct std::_Bind_simple<void (*(std::array<std::array<int, 5ul>, 4ul>, int, int))(std::array<std::array<int, 5ul>, 4ul>&, long unsigned int, long unsigned int)>’:
/usr/include/c++/4.8/thread:137:47: required from ‘std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (&)(std::array<std::array<int, 5ul>, 4ul>&, long unsigned int, long unsigned int); _Args = {std::array<std::array<int, 5ul>, 4ul>&, const int&, const int&}]’
Testo.cpp:13:82: required from here
/usr/include/c++/4.8/functional:1697:61: error: no type named ‘type’ in ‘class std::result_of<void (*(std::array<std::array<int, 5ul>, 4ul>, int, int))(std::array<std::array<int, 5ul>, 4ul>&, long unsigned int, long unsigned int)>’
typedef typename result_of<_Callable(_Args...)>::type result_type;
^
/usr/include/c++/4.8/functional:1727:9: error: no type named ‘type’ in ‘class std::result_of<void (*(std::array<std::array<int, 5ul>, 4ul>, int, int))(std::array<std::array<int, 5ul>, 4ul>&, long unsigned int, long unsigned int)>’
_M_invoke(_Index_tuple<_Indices...>)
^
我可以将模板更改为
template <class TYPE>
void function(TYPE data, std::size_t row, std::size_t column){
data[row-1][column-1]=2;
}
并且编译器不再抱怨,但是 main 然后 returns 0 因为数组不再通过引用传递。 线程调用正确模板以通过引用传递数组的适当第一个参数是什么,就像直接调用函数时一样?
通过引用传递 std::ref(arr)
您的函数需要对数组的引用,但 std::thread
存储绑定参数的衰减副本。数组到指针的衰减导致指针类型的纯右值。要修复,请通过 std::reference_wrapper
传递 arr
以保留类型:
std::thread worker(..., std::ref(arr), row, column);
您可能还想使用 lambda,这样就不必显式传递模板参数:
std::thread worker([&arr]{ return function(arr, row, column); });