io_service相当于注册异步回调的一个上下文环境,而run相当于处理异步io的上下文(通常是一个线程)。

单io_service,多线程run,相当于多个线程同时来处理注册在一个io_service上的回调:

//sio_mth.cpp#include #include #include #include #include #include using namespace boost::asio;using namespace std; thread_local int t_id = 0;unsigned long getTimestamp(){return std::chrono::system_clock::now().time_since_epoch().count()/std::chrono::system_clock::period::den;} void timer_handler(int timerID, const boost::system::error_code& err){if (err){cout << getTimestamp() << " Timer cancel" << endl;return;}cout << getTimestamp() <<" t_id:" << t_id << " timerID:" << timerID << " " << " Time