implementing future::then() equivalent for asynchronous execution in c++11

前端 未结 3 1519
既然无缘
既然无缘 2020-12-04 17:55

I have a few questions about the implementation of the function then() in Herb Sutter\'s talk. This function is used to chain asynchronous operations, the param

相关标签:
3条回答
  • 2020-12-04 18:31

    no, it is not correct. if you pass return value of .get() to continuation, it will not be able to handle exception, propagated from .get(). you have to pass future to continuation, and call .get() manually, just like in boost.thread

    0 讨论(0)
  • 2020-12-04 18:34

    In order to simplify the interface, I would "hide" the void problem inside the implementation, similarly to what Herb did with his concurrent<T> implementation. Instead of having 2 then implementations, declare a helper function get_work_done with 2 implementations:

    template <typename T, typename Work>
    auto get_work_done(future<T> &f, Work &w)-> decltype(w(f.get()))
    {return w(f.get());}
    
    template <typename Work>
    auto get_work_done(future<void> &f, Work &w)-> decltype(w())
    {f.wait(); return w();}
    

    And then let template parameter detection take care of the rest:

    template <typename T, typename Work>
    auto then(future<T> f, Work w) -> future<decltype(w(f.get()))>
    {
        return async([](future<T> f, Work w)
                          { return get_work_done(f,w); }, move(f), move(w));
    }
    
    0 讨论(0)
  • 2020-12-04 18:36

    The problem with this approach to .then() is that you spawn 2 threads (that is costly) simultaneously, and second of them would block on its future.get/wait (if the first one would run long enough, of course) So, its better to use the work queue, to serialize the jobs execution order (and re-cycle the existing threads). Just look for a good Thread pool pattern implementation

    0 讨论(0)
提交回复
热议问题