我正在尝试为我的应用程序使用CPTL线程池。
所以,我有这个定义的功能:
static void Invoke( int id, std::unique_ptr<BaseService> svc );
并尝试将其传递给CPTL“推”方法以在线程池中排队:
pThreadPool->push( std::ref(App::Invoke), std::move( svc ) );
但是我收到了这个错误:
/home/hadi/CLionProjects/App/App.cpp:211:27: error: no matching member function for call to 'push'
pThreadPool->push( std::ref(App::Invoke), std::move( svc ) );
~~~~~~~~~~~~~^~~~
/home/hadi/CLionProjects/App/include/cptl/ctpl.h:152:14: note: candidate template ignored: substitution failure [with F = std::__1::reference_wrapper<void (int, std::__1::unique_ptr<BaseService, std::__1::default_delete<BaseService> >)>, Rest = <std::__1::unique_ptr<BaseService, std::__1::default_delete<BaseService> >>]: call to implicitly-deleted copy constructor of 'std::__1::unique_ptr<BaseService, std::__1::default_delete<BaseService> >'
auto push(F && f, Rest&&... rest) ->std::future<decltype(f(0, rest...))> {
^ ~~~~
/home/hadi/CLionProjects/App/include/cptl/ctpl.h:171:14: note: candidate function template not viable: requires single argument 'f', but 2 arguments were provided
auto push(F && f) ->std::future<decltype(f(0))> {
^
1 error generated.
有人可以告诉我如何解决这个问题吗?谢谢。
看来CPTL在第152行有一个bug或不支持仅移动参数(无法在其上找到任何文档,所以不能确定):
auto push(F && f, Rest&&... rest) ->std::future<decltype(f(0, rest...))> {
即使push
使用std::forward<Rest>(rest)...
在调用,它使用rest...
在SFINAE,这将失败,不包括从可行的候选人名单的过载。
通常,打包任务的参数存储在队列中,因此需要可复制。unique_ptr
由于它不可复制(这是唯一的!),因此排除了使用。
作为解决方法,您可以使用shared_ptr
或,如果生存期svc
超过了线程池的生存期,则可以使用原始指针。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句