为什么我们需要使用boost :: asio :: io_service :: work? [英] Why do we need to use boost::asio::io_service::work?

查看:1483
本文介绍了为什么我们需要使用boost :: asio :: io_service :: work?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个使用boost :: asio的例子。

There is an example of using boost::asio.


  1. 为什么这个例子使用boost :: asio :: io_service: :work?

  2. 为什么不调用 srv.run(); 在线程中执行任务?

  1. Why does this example use the boost::asio::io_service::work ?
  2. And why is srv.run (); not called to perform tasks in the threads?





int main()
{
    boost::asio::io_service srv;
    boost::asio::io_service::work work(srv);
    boost::thread_group thr_grp;
    thr_grp.create_thread(boost::bind(&boost::asio::io_service::run, &srv));
    thr_grp.create_thread(boost::bind(&boost::asio::io_service::run, &srv));

    srv.post(boost::bind(f1, 123));
    srv.post(boost::bind(f1, 321));
    //sync

    srv.post(boost::bind(f2, 456));
    srv.post(boost::bind(f2, 654));
    //sync

    srv.stop();
    thr_grp.join();
}

更新:
使用io_service而不使用io_service :: work时,轮询和运行之间的区别?

Update: What is the difference between the poll and run, when io_service is used without io_service::work?

int main()
{
    boost::asio::io_service srv;
    //boost::asio::io_service::work work(srv);
    std::vector<boost::thread> thr_grp;

    srv.post(boost::bind(f1, 123));
    srv.post(boost::bind(f1, 321));
    //sync

    srv.post(boost::bind(f2, 456));
    srv.post(boost::bind(f2, 654));
    //sync

    // What is the difference between the poll and run, when io_service without work?
    thr_grp.emplace_back(boost::bind(&boost::asio::io_service::poll, &srv));// poll or run?
    thr_grp.emplace_back(boost::bind(&boost::asio::io_service::run, &srv));// poll or run? 

    srv.stop();
    for(auto &i : thr_grp) i.join();

    int b;
    std::cin >> b;

    return 0;
}


推荐答案

当io_service :: run方法被调用没有工作对象,它会立即返回。通常,这不是大多数开发人员正在寻找的行为。当然有一些例外,但大多数开发人员希望指定一个线程来处理所有的异步处理,不希望该线程退出,直到被告知这样做。这是你的代码示例所做的。

When the io_service::run method is called without a work object, it will return right away. Typically, that is not the behavior most developers are looking for. There are of course some exceptions, but most developers are looking to specify a thread to handle all of the asynchronous processing and don't want that thread to exit until told to do so. That is what your code example does.

io_service :: run方法被指定为create_thread方法中的委托或函数指针。因此,当线程从create_thread方法创建时,它将调用io_service :: run方法,并将io_service对象作为参数传递。通常,一个io_service对象可以与多个套接字对象一起使用。

The io_service::run method is specified as a delegate or function pointer in the create_thread methods. So, when the thread is created from the create_thread method it will call the io_service::run method and it passes the io_service object as an argument. Typically one io_service object can be used with multiple socket objects.

停止方法通常在关闭应用程序或不再需要所有客户端/服务器之间通信时调用并且不期望任何新的连接将需要启动。

The stop method is usually called when shutting down the application or when communication between all clients/servers is no longer required and it is not anticipated that any new connections will need to be initiated.

这篇关于为什么我们需要使用boost :: asio :: io_service :: work?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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