正确使用模板将函数传递给线程池

时间:2018-07-21 19:28:10

标签: c++ templates c++17 decltype

我正在尝试创建一个具有任何类型的函数的线程池,并为该函数可能具有的任何返回值/异常返回一个Future。我主要是通过练习来学习现代线程和一些模板编程。我试图根据MSVC的std :: function和Futures功能来轻松地构建语法。

这是我的问题所在的最低摘要:

#include <functional>
#include <future>
#include <utility>
#include <queue>
#include <memory>

using Job = std::function<void()>;

std::queue<std::unique_ptr<Job>> queue;

template<typename FuncType, typename... Args>
auto add(FuncType&& func, Args&&... args) ->std::future<decltype(func)(decltype(args)...)> {
    auto task = std::packaged_task<decltype(func)(decltype(args)...)>(std::bind (std::forward<FuncType>(func), std::forward<Args>(args)...));
    auto future = task.get_future();

    queue.push(std::make_unique<Job>([task]() { task(); }));

    return future;
}

void voidFunc(){};

int main()
{
    add(voidFunc);
}

这无法编译并显示以下错误:

    /usr/include/c++/4.9/future: In instantiation of 'class std::future<void (&())()>':
28:17:   required from here
/usr/include/c++/4.9/future:697:7: error: function returning a function
       get()
       ^
 In instantiation of 'add(FuncType&&, Args&& ...)::<lambda()> [with FuncType = void (&)(); Args = {}]':
19:37:   required from 'struct add(FuncType&&, Args&& ...) [with FuncType = void (&)(); Args = {}; decltype (func) = void (&)()]::<lambda()>'
19:56:   required from 'std::future<decltype (func)(decltype (args)...)> add(FuncType&&, Args&& ...) [with FuncType = void (&)(); Args = {}; decltype (func) = void (&)()]'
28:17:   required from here
19:52: error: passing 'const std::packaged_task<void (&())()>' as 'this' argument of 'void std::packaged_task<_Res(_ArgTypes ...)>::operator()(_ArgTypes ...) [with _Res = void (&)(); _ArgTypes = {}]' discards qualifiers [-fpermissive]
 In instantiation of 'std::future<decltype (func)(decltype (args)...)> add(FuncType&&, Args&& ...) [with FuncType = void (&)(); Args = {}; decltype (func) = void (&)()]':
28:17:   required from here
19:36: error: use of deleted function 'std::packaged_task<_Res(_ArgTypes ...)>::packaged_task(const std::packaged_task<_Res(_ArgTypes ...)>&) [with _Res = void (&)(); _ArgTypes = {}]'
In file included from 5:0:
/usr/include/c++/4.9/future:1413:7: note: declared here
       packaged_task(const packaged_task&) = delete;
       ^
21:10: error: could not convert 'future' from 'std::future<void (&)()>' to 'std::future<void (&())()>'
In file included from 5:0:
/usr/include/c++/4.9/future: In instantiation of 'static std::__future_base::_Task_setter<_Res_ptr> std::__future_base::_S_task_setter(_Res_ptr&, _BoundFn&&) [with _Res_ptr = std::unique_ptr<std::__future_base::_Result<void (&)()>, std::__future_base::_Result_base::_Deleter>; _BoundFn = std::_Bind_simple<std::reference_wrapper<std::_Bind<void (*())()> >()>; typename _Res_ptr::element_type::result_type = void (&)()]':
/usr/include/c++/4.9/future:1318:70:   required from 'void std::__future_base::_Task_state<_Fn, _Alloc, _Res(_Args ...)>::_M_run(_Args ...) [with _Fn = std::_Bind<void (*())()>; _Alloc = std::allocator<int>; _Res = void (&)(); _Args = {}]'
29:1:   required from here
/usr/include/c++/4.9/future:539:57: error: could not convert 'std::ref(_Tp&) [with _Tp = std::_Bind_simple<std::reference_wrapper<std::_Bind<void (*())()> >()>]()' from 'std::reference_wrapper<std::_Bind_simple<std::reference_wrapper<std::_Bind<void (*())()> >()> >' to 'std::function<void (&())()>'
  return _Task_setter<_Res_ptr>{ __ptr, std::ref(__call) };
                                                         ^
In file included from /usr/include/c++/4.9/memory:81:0,
                 from /usr/include/c++/4.9/thread:40,
                 from /usr/include/c++/4.9/future:40,
                 from 5:
/usr/include/c++/4.9/bits/unique_ptr.h:764:5: error: 'typename std::_MakeUniq<_Tp>::__single_object std::make_unique(_Args&& ...) [with _Tp = std::function<void()>; _Args = {add(FuncType&&, Args&& ...) [with FuncType = void (&)(); Args = {}; decltype (func) = void (&)()]::<lambda()>}; typename std::_MakeUniq<_Tp>::__single_object = std::unique_ptr<std::function<void()> >]', declared using local type 'add(FuncType&&, Args&& ...) [with FuncType = void (&)(); Args = {}; decltype (func) = void (&)()]::<lambda()>', is used but never defined [-fpermissive]
     make_unique(_Args&&... __args)
     ^
 In function 'std::future<decltype (func)(decltype (args)...)> add(FuncType&&, Args&& ...) [with FuncType = void (&)(); Args = {}; decltype (func) = void (&)()]':
22:2: warning: control reaches end of non-void function [-Wreturn-type]

我认为我在这里有两个问题:我不知道如何正确使用decltype来获取正确的函数签名(我也研究了invoke_result,但是我那里也没有运气),我认为我可能也没有将打包的任务正确地传递到队列。

如何为将来的打包任务获取正确的函数签名,以及如何将打包任务正确传递给队列中的std :: function(稍后将被另一个线程捕获)?

1 个答案:

答案 0 :(得分:2)

两个问题:

1)打包任务的签名应为 std::packaged_task<std::invoke_result_t<Func&&,Args&&...>(Args&&...)>。这使用invoke_result_t计算返回类型,但也将参数类型传递给打包的任务。

2)更大的问题:std::function要求该函数是可复制构造的,而std::packaged_task则不是。您必须创建自己的队列来保存打包的任务。在使用基类Task和带有模板的派生类来容纳打包的任务之前,我已经实现了它。