How can I get a future from boost::asio::post?

后端 未结 2 1352
温柔的废话
温柔的废话 2021-01-01 00:29

I am using Boost 1.66.0, in which asio has built-in support for interoperating with futures (and for some time now). The examples I\'ve seen online indicate how to achieve t

2条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-01 00:54

    What kind of object do I need to provide or wrap my function in to get the same behavior from boost::asio::post?

    You can't. post is a void operation. So the only option to achieve it with post is to use a packaged-task, really.

    The Real Question

    It was hidden in the part "how to get the same behaviour" (just not from post):

    template 
    auto async_meaning_of_life(bool success, Token&& token)
    {
        using result_type = typename asio::async_result, void(error_code, int)>;
        typename result_type::completion_handler_type handler(std::forward(token));
    
        result_type result(handler);
    
        if (success)
            handler(error_code{}, 42);
        else
            handler(asio::error::operation_aborted, 0);
    
        return result.get ();
    }
    

    You can use it with a future:

    std::future f = async_meaning_of_life(true, asio::use_future);
    std::cout << f.get() << "\n";
    

    Or you can just use a handler:

    async_meaning_of_life(true, [](error_code ec, int i) {
        std::cout << i << " (" << ec.message() << ")\n";
    });
    

    Simple demo: Live On Coliru

    Extended Demo

    The same mechanism extends to supporting coroutines (with or without exceptions). There's a slightly different dance with async_result for Asio pre-boost 1.66.0.

    See all the different forms together here:

    • How to set error_code to asio::yield_context

    Live On Coliru

    #define BOOST_COROUTINES_NO_DEPRECATION_WARNING 
    #include 
    #include 
    #include 
    #include 
    
    using boost::system::error_code;
    namespace asio = boost::asio;
    
    template 
    auto async_meaning_of_life(bool success, Token&& token)
    {
    #if BOOST_VERSION >= 106600
        using result_type = typename asio::async_result, void(error_code, int)>;
        typename result_type::completion_handler_type handler(std::forward(token));
    
        result_type result(handler);
    #else
        typename asio::handler_type::type
                     handler(std::forward(token));
    
        asio::async_result result (handler);
    #endif
    
        if (success)
            handler(error_code{}, 42);
        else
            handler(asio::error::operation_aborted, 0);
    
        return result.get ();
    }
    
    void using_yield_ec(asio::yield_context yield) {
        for (bool success : { true, false }) {
            boost::system::error_code ec;
            auto answer = async_meaning_of_life(success, yield[ec]);
            std::cout << __FUNCTION__ << ": Result: " << ec.message() << "\n";
            std::cout << __FUNCTION__ << ": Answer: " << answer << "\n";
        }
    }
    
    void using_yield_catch(asio::yield_context yield) {
        for (bool success : { true, false }) 
        try {
            auto answer = async_meaning_of_life(success, yield);
            std::cout << __FUNCTION__ << ": Answer: " << answer << "\n";
        } catch(boost::system::system_error const& e) {
            std::cout << __FUNCTION__ << ": Caught: " << e.code().message() << "\n";
        }
    }
    
    void using_future() {
        for (bool success : { true, false }) 
        try {
            auto answer = async_meaning_of_life(success, asio::use_future);
            std::cout << __FUNCTION__ << ": Answer: " << answer.get() << "\n";
        } catch(boost::system::system_error const& e) {
            std::cout << __FUNCTION__ << ": Caught: " << e.code().message() << "\n";
        }
    }
    
    void using_handler() {
        for (bool success : { true, false })
            async_meaning_of_life(success, [](error_code ec, int answer) {
                std::cout << "using_handler: Result: " << ec.message() << "\n";
                std::cout << "using_handler: Answer: " << answer << "\n";
            });
    }
    
    int main() {
        asio::io_service svc;
    
        spawn(svc, using_yield_ec);
        spawn(svc, using_yield_catch);
        std::thread work([] {
                using_future();
                using_handler();
            });
    
        svc.run();
        work.join();
    }
    

    Prints

    using_yield_ec: Result: Success
    using_yield_ec: Answer: 42
    using_yield_ec: Result: Operation canceled
    using_yield_ec: Answer: 0
    using_yield_catch: Answer: 42
    using_future: Answer: 42
    using_yield_catch: Caught: Operation canceled
    using_future: Answer: using_future: Caught: Operation canceled
    using_handler: Result: Success
    using_handler: Answer: 42
    using_handler: Result: Operation canceled
    using_handler: Answer: 0
    

提交回复
热议问题