进入正题,简要说一下asio的实现原理吧。在win32平台上,asio是基于IOCP技术实现的,我以前也用过IOCP,却没想到居然能扩展成这样,真是神奇!在其他平台下还会有别的方法去实现,具体见io_service类下面这部分的源码:

// The type of the platform-specific implementation.

#if defined(BOOST_ASIO_HAS_IOCP)

typedef detail::win_iocp_io_service impl_type;

friend class detail::win_iocp_overlapped_ptr;

#elif defined(BOOST_ASIO_HAS_EPOLL)

typedef detail::task_io_service<detail::epoll_reactor<false> > impl_type;

#elif defined(BOOST_ASIO_HAS_KQUEUE)

typedef detail::task_io_service<detail::kqueue_reactor<false> > impl_type;

#elif defined(BOOST_ASIO_HAS_DEV_POLL)

typedef detail::task_io_service<detail::dev_poll_reactor<false> > impl_type;

#else

typedef detail::task_io_service<detail::select_reactor<false> > impl_type;

#endif
这部分代码其实就在boost::asio::io_service类声明中的最前面几行,可以看见在不同平台下,io_service类的实现将会不同。很显