io_context的具体实现如下代码,io_context最主要的函数为:
(1) 构造函数,其中参数控制并行的线程数,对于性能比较重要,具体如下,实际上,就是构造函数的参数为1或者为BOOST_ASIO_CONCURRENCY_HINT_ID时,采用单线程,默认情况下都是采用多线程(对于普通应用场景,不用关心这个参数,但是,对于一些性能限制的设备,如果采用boost::asio时,需要考虑,实际上,在这种场景下,可以直接使用系统API进行通信也许是更好的选择)
// The concurrency hint ID and mask are used to identify when a "well-known"
// concurrency hint value has been passed to the io_context.
#define BOOST_ASIO_CONCURRENCY_HINT_ID 0xA5100000u
#define BOOST_ASIO_CONCURRENCY_HINT_ID_MASK 0xFFFF0000u
// If set, this bit indicates that the scheduler should perform locking.
#define BOOST_ASIO_CONCURRENCY_HINT_LOCKING_SCHEDULER 0x1u
// If set, this bit indicates that the reactor should perform locking when
// managing descriptor registrations.
#define BOOST_ASIO_CONCURRENCY_HINT_LOCKING_REACTOR_REGISTRATION 0x2u
// If set, this bit indicates that the reactor should perform locking for I/O.
#define BOOST_ASIO_CONCURRENCY_HINT_LOCKING_REACTOR_IO 0x4u
// Helper macro to determine if we have a special concurrency hint.
#define BOOST_ASIO_CONCURRENCY_HINT_IS_SPECIAL(hint) \
((static_cast<unsigned>(hint) \
& BOOST_ASIO_CONCURRENCY_HINT_ID_MASK) \
== BOOST_ASIO_CONCURRENCY_HINT_ID)
// Helper macro to determine if locking is enabled for a given facility.
#define BOOST_ASIO_CONCURRENCY_HINT_IS_LOCKING(facility, hint) \
(((static_cast<unsigned>(hint) \
& (BOOST_ASIO_CONCURRENCY_HINT_ID_MASK \
| BOOST_ASIO_CONCURRENCY_HINT_LOCKING_ ## facility)) \
^ BOOST_ASIO_CONCURRENCY_HINT_ID) != 0)
// This special concurrency hint disables locking in both the scheduler and
// reactor I/O. This hint has the following restrictions:
//
// - Care must be taken to ensure that all operations on the io_context and any
// of its associated I/O objects (such as sockets and timers) occur in only
// one thread at a time.
//
// - Asynchronous resolve operations fail with operation_not_supported.
//
// - If a signal_set is used with the io_context, signal_set objects cannot be
// used with any other io_context in the program.
#define BOOST_ASIO_CONCURRENCY_HINT_UNSAFE \
static_cast<int>(