Using futures with boost::asio

后端 未结 2 779
小鲜肉
小鲜肉 2021-02-08 10:16

Does anyone have a good pointer to examples which use futures from the Boost thread library with Boost ASIO? I have an existing asynchronous library which uses callback function

2条回答
  •  悲&欢浪女
    2021-02-08 10:43

    Can you please look at this example:

    http://www.boost.org/doc/libs/1_61_0/doc/html/boost_asio/example/cpp11/futures/daytime_client.cpp

    It shows how to use std::future with boost asio.

    The key point is to include file use_future.hpp:

    #include 
    

    Then you can write code like that:

    std::future my_future =
        my_socket.async_read_some(my_buffer, boost::asio::use_future);
    

    If you need to use boost::future then I'd suggest to implement another variant, similar to boost::asio::use_future.
    The file use_future.hpp is a good example for that.

提交回复
热议问题