Boost 1.70 io_service弃用 [英] Boost 1.70 io_service deprecation

查看:146
本文介绍了Boost 1.70 io_service弃用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一些旧代码从使用io_service迁移到基本tcp接受器的io_context,但是在将get_io_service()切换到get_executor()时遇到了问题.context()导致以下错误:

I'm trying to migrate some old code from using io_service to io_context for the basic tcp acceptor, but am running into issues when switching get_io_service() to get_executor().context() results in the following error:

cannot convert ‘boost::asio::execution_context’ to ‘boost::asio::io_context&’

这是侦听器:

ImageServerListener::ImageServerListener(boost::asio::io_context& io)
{
    _acceptor = new boost::asio::ip::tcp::acceptor(io, boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), sConfig.net.imageServerPort));
    StartAccept();
}

ImageServerListener::~ImageServerListener()
{
    delete _acceptor;
}

void ImageServerListener::StartAccept()
{
    std::shared_ptr<ImageServerConnection> connection = ImageServerConnection::create(_acceptor->get_executor().context());
    _acceptor->async_accept(connection->socket(), std::bind(&ImageServerListener::HandleAccept, this, connection));
}

void ImageServerListener::HandleAccept(std::shared_ptr<ImageServerConnection> connection)
{
    connection->Process();
    StartAccept();
}

为了返回io_context而不是execute_context,必须进行哪些更改?

What would have to be changed in order to return an io_context instead of an execution_context?

推荐答案

您将要专注于执行者,而不是上下文.

You will want to focus on executors rather than contexts.

与执行者相比,传递执行者很便​​宜,它们是可复制的.

Passing around executors is cheap, they are copyable, as opposed to contexts.

此外,它抽象出(多态性)执行器附加到的执行上下文的类型,因此您无需打扰.

Also, it abstracts away (polymorphism) the type of execution context that the executor is attached to, so you don't need to bother.

但是,执行程序的 static 类型不是固定的.这意味着典型的接受方式是通过模板参数:

However, the static type of the executor is not fixed. This means that the typical way to accept one is by template argument:

struct MyThing {
    template <typename Executor>
    explicit MyThing(Executor ex)
       : m_socket(ex)
    { }

    void do_stuff(std::string caption) {
        post(m_socket.get_executor(),
            [=] { std::cout << ("Doing stuff " + caption + "\n") << std::flush; });
    }

    // ...
  private:
    tcp::socket m_socket;
};

现在,您可以通过多种方式使用它,而无需进行更改:

Now you employ it in many ways without changes:

在Coliru上直播

Live On Coliru

int main() {
    boost::asio::thread_pool pool;
    MyThing a(pool.get_executor());
    MyThing b(make_strand(pool));

    a.do_stuff("Pool a");
    b.do_stuff("Pool b");

    boost::asio::io_context ioc;
    MyThing c(ioc.get_executor());
    MyThing d(make_strand(ioc));

    c.do_stuff("IO c");
    d.do_stuff("IO d");

    pool.join();
    ioc.run();
}

哪个会打印类似

Doing stuff Pool a
Doing stuff Pool b
Doing stuff IO c
Doing stuff IO d

类型擦除

您可能已经猜到了,在 m_socket 中有一个类型擦除,用于存储执行程序.如果您想这样做,可以使用

Type Erasure

As you have probably surmised, there's type erasure inside m_socket that stores the executor. If you want to do the same, you can use

boost::asio::any_io_executor ex;
ex = m_socket.get_executor();

这篇关于Boost 1.70 io_service弃用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆