Skip to content

async_write with Future && async result #226

Description

@basiliscos

Hi,

the following code works on boost 1.70, but on 1.69 it throws with std::future_error: Future already retrieved .

I cannot see any reference to in the changelog that it was fixed, and still not completely sure the code sample is correct.

Could you, please, leave a comment?

#include <boost/asio.hpp>
#include <boost/asio/use_future.hpp>
#include <future>
#include <iostream>

namespace asio = boost::asio;

template<typename Stream, typename Buff, typename CompletionToken>
auto async_op(Stream &s, Buff&& buff, CompletionToken&& ct) {
    using Signature = void(const boost::system::error_code&, std::size_t);
    using Callback = std::decay_t<CompletionToken>;
    using AsyncResult = asio::async_result<Callback, Signature>;
    using CompletionHandler = typename AsyncResult::completion_handler_type;

    CompletionHandler handler(std::forward<CompletionToken>(ct));
    AsyncResult result(handler);
    async_write(s, std::move(buff), std::move(handler));
    return result.get();
}

int main(int argc, char** argv) {
    using socket_t = asio::ip::tcp::socket;

    asio::io_service io_service;

    asio::ip::tcp::resolver resolver(io_service);
    socket_t sock(io_service);
    auto endpoints = resolver.resolve("google.com", "https");
    sock.connect(*endpoints.begin());
    char buff[] = "does-not-matter";

    auto f_write = async_op(sock, asio::buffer(buff), asio::use_future);
    //auto f_write = async_write(sock, asio::buffer(buff), asio::use_future);
    std::thread thread([&io_service]() { io_service.run(); });

    std::cout << "r = " << f_write.get() << "\n";

    io_service.stop();
    thread.join();

    return  0;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions