io_context

Like traditional Unix network programming, Boost.Asioalso has “socket” concept, but that’s not enough, an io_context object (io_service class is deprecated now) is needed to communicate with Operating System’s I/O services. The architecture is like following:
image

io_context derives from execution_context:

  1. class io_context
  2. : public execution_context
  3. {
  4. ......
  5. }

While execution_context derives from noncopyable:

  1. class execution_context
  2. : private noncopyable
  3. {
  4. ......
  5. }

Check noncopyable class definition:

  1. class noncopyable
  2. {
  3. protected:
  4. noncopyable() {}
  5. ~noncopyable() {}
  6. private:
  7. noncopyable(const noncopyable&);
  8. const noncopyable& operator=(const noncopyable&);
  9. };

It means the io_context object can’t be copy constructed/copy assignment/move constructed/move assignment, So during initialization of socket, i.e., associate socket with io_context, the io_context should be passed as a reference. E.g.:

  1. template <typename Protocol
  2. BOOST_ASIO_SVC_TPARAM_DEF1(= datagram_socket_service<Protocol>)>
  3. class basic_datagram_socket
  4. : public basic_socket<Protocol BOOST_ASIO_SVC_TARG>
  5. {
  6. public:
  7. ......
  8. explicit basic_datagram_socket(boost::asio::io_context& io_context)
  9. : basic_socket<Protocol BOOST_ASIO_SVC_TARG>(io_context)
  10. {
  11. }
  12. ......
  13. }