所以,我有这段代码,应该作为一个单独的线程运行:
template<class Iterator>
void thread_launcher(Iterator start, Iterator fin, size_t sort_type) {
// Blah blah
mixed_sort<less<int>>(start, fin, sort_type);
// blah blah
}
这段代码应该创建这个线程:
for (size_t sort_type = 2; sort_type!= 7; ++sort_type) {
// blah
t[sort_type] = thread(thread_launcher, copy.begin(), copy.end(), sort_type);
}
,其中t
是的数组thread
。
问题是---当我尝试编译它时,出现以下错误:
main.cpp:245:32: error: no matching constructor for initialization of
'std::__1::thread'
...= thread(thread_launcher, copy.begin(), copy.end(), sort_type);
^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/thread:349:9: note:
candidate template ignored: couldn't infer template argument '_Fp'
thread::thread(_Fp&& __f, _Args&&... __args)
我不明白我做错了什么,尽管这是我第一次使用多线程,更不用说了std::thread
。我应该怎么做才能解决这个问题?
不确定是否没有所有细节,但令人惊讶的是您正在thread_launcher
作为参数传递。这不是功能,而是功能模板。
考虑以下:
template<typename T>
void foo(T)
{
}
int main()
{
thread t(foo<int>, 3);
return 0;
}
在我看来,这很重要,但是当我将其更改为
thread t(foo, 3);
它无法建立。
然后,您可能希望将自己更改为
thread(
thread_launcher<decltype(begin(copy))>,
begin(copy),
end(copy),
sort_type)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句