You should use async_read_some instead of read_some , as it allows you to start a new background timer while reading. Then, to create a new timer for each new socket:
boost::asio::io_service io_service; time_t_timer timer(io_service); timer.expires_from_now(333); std::cout << "Starting asynchronous wait\n"; timer.async_wait(&handle_timeout); io_service.run();
You will have two asynchronous calls waiting on the background.
Whenever you get some timer data, you can reset the countdown with cancel and expires_from_now , and when the timer expires, you can close the socket or perform another action.
source share