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

前端 未结 3 1534
既然无缘
既然无缘 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:34

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

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

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

    template 
    auto then(future f, Work w) -> future
    {
        return async([](future f, Work w)
                          { return get_work_done(f,w); }, move(f), move(w));
    }
    

提交回复
热议问题