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

后端 未结 2 1368
温柔的废话
温柔的废话 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:50

    Thas what I came up with, it essentially wrapts the asio::post and plugs in a promise/future pair. I think it can be adapted to your needs as well.

    // outer scope setup
    asio::io_context context;
    asio::io_context::strand strand(context);
    
    
    std::future async_send(tcp::socket& socket, std::string message) {
        auto buffered = std::make_shared(message);
        std::promise promise;
        auto future = promise.get_future();
    
        // completion handler which only sets the promise.
        auto handler = [buffered, promise{std::move(promise)}](asio::error_code, std::size_t) mutable {
            promise.set_value();
        };
    
        // post async_write call to strand. Thas *should* protecte agains concurrent 
        // writes to the same socket from multiple threads
        asio::post(strand, [buffered, &socket, handler{std::move(handler)}]() mutable {
            asio::async_write(socket, asio::buffer(*buffered), asio::bind_executor(strand, std::move(handler)));
        });
        return future;
    }
    

    The promise can be moved without the future becoming invalidated.

    Adapted to your scenario it could be somethign like this:

    template
    std::future post_with_future(C&& handler)
    {
        std::promise promise;
        auto future = promise.get_future();
    
        auto wrapper = [promise{std::move(promise)}]{ // maybe mutable required?
             handler();
             promise.set_value();
        };
    
        // need to move in, cause the promise needs to be transferred. (i think)
        asio::post(strand, std::move(wrapper)); 
        return future;
    }
    

    I would be happy about some feedback to those lines, as I am myself just learning the whole thing :)

    Hope to help, Marti

提交回复
热议问题