问题描述
我是 std::thread
的新手,我正在努力找出我做错了什么。
这个想法是在 DataUtils
类中有一个静态函数,它将程序分成 10 个线程,每个线程将计算两个 vectors
之间卷积的“一部分”。
由于某种原因,编译器似乎无法为线程分配正确的函数,但参数是正确的。
这是我的代码(我相信有一种更优雅的方法来做到这一点......)
void threadconv (std::vector<double> const &f,std::vector<double> const &g,std::vector<double> &out,int start,int stop,int nf,int ng){
for(auto i(start); i < stop; ++i) {
int const jmn = (i >= ng - 1)? i - (ng - 1) : 0;
int const jmx = (i < nf - 1)? i : nf - 1;
for(auto j(jmn); j <= jmx; ++j) {
out[i] += (f[j] * g[i - j]);
}
}
}
class DataUtils{
static std::vector<double> multiThreadedConvolution(std::vector<double> const &f,int divide){
int const nf = f.size();
int const ng = g.size();
int const n = nf + ng - 1;
int i=0;
std::vector<double> out(n,double());
std::thread t1 (threadconv,f,g,out,divide,nf,ng);i++;
std::thread t2 (threadconv,divide*i,(divide*i)+divide,ng);i++;
std::thread t3 (threadconv,ng);i++;
std::thread t4 (threadconv,ng);i++;
std::thread t5 (threadconv,ng);i++;
std::thread t6 (threadconv,ng);i++;
std::thread t7 (threadconv,ng);i++;
std::thread t8 (threadconv,ng);i++;
std::thread t9 (threadconv,ng);i++;
std::thread t10 (threadconv,out.size(),ng);
t1.join();
t2.join();
t3.join();
t4.join();
t5.join();
t6.join();
t7.join();
t8.join();
t9.join();
t10.join();
return out;
}
}
尝试编译后,这是我收到的错误消息:
/usr/include/c++/7/thread:240:2: error: no matching function for call to ‘std::thread::_Invoker<std::tuple<void (*)(const std::vector<double,std::allocator<double> >&,const std::vector<double,std::vector<double,int,int),std::allocator<double> >,int> >::_M_invoke(std::thread::_Invoker<std::tuple<void (*)(const std::vector<double,int> >::_Indices)’
operator()()
^~~~~~~~
/usr/include/c++/7/thread:231:4: note: candidate: template<long unsigned int ..._Ind> decltype (std::__invoke((_S_declval<_Ind>)()...)) std::thread::_Invoker<_Tuple>::_M_invoke(std::_Index_tuple<_Ind ...>) [with long unsigned int ..._Ind = {_Ind ...}; _Tuple = std::tuple<void (*)(const std::vector<double,int>]
_M_invoke(_Index_tuple<_Ind...>)
^~~~~~~~~
/usr/include/c++/7/thread:231:4: note: template argument deduction/substitution Failed:
/usr/include/c++/7/thread: In substitution of ‘template<long unsigned int ..._Ind> decltype (std::__invoke(_S_declval<_Ind>()...)) std::thread::_Invoker<std::tuple<void (*)(const std::vector<double,int> >::_M_invoke<_Ind ...>(std::_Index_tuple<_Ind1 ...>) [with long unsigned int ..._Ind = {0,1,2,3,4,5,6,7}]’:
/usr/include/c++/7/thread:240:2: required from ‘struct std::thread::_Invoker<std::tuple<void (*)(const std::vector<double,int> >’
/usr/include/c++/7/thread:127:22: required from ‘std::thread::thread(_Callable&&,_Args&& ...) [with _Callable = void (&)(const std::vector<double>&,const std::vector<double>&,std::vector<double>&,int); _Args = {const std::vector<double,int&,const int&,const int&}]’
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)