Why do we need to use boost::asio::io_service::work?

前端 未结 1 615
北恋
北恋 2020-11-27 06:34

There is an example of using boost::asio.

  1. Why does this example use the boost::asio::io_service::work ?
  2. And why is srv.run (); not called
相关标签:
1条回答
  • 2020-11-27 07:20

    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.

    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.

    0 讨论(0)
提交回复
热议问题