Receiving or sending data by network is a slow operation. While packets are received by the machine, and while OS verifies them and copies the data to the user-specified buffer, multiple seconds may pass.
We may do a lot of work rather than waiting! Let's modify our tasks_processor
class so that it would be capable of sending and receiving data in an asynchronous manner. In nontechnical terms, we ask it to receive at least N bytes from the remote host and after that is done, call our functor. By the way, do not block on this call. Those readers who know about libev, libevent, or Node.js may find a lot of familiar things in this recipe.
This recipe is based on the previous two recipes. See the first recipe of this chapter to get information about the boost::asio::io_service
and task_queue
classes. See the second recipe review the basics of async processing.
Link this recipe with the boost_system
and boost_thread
libraries. Define BOOST_ASIO_DISABLE_HANDLER_TYPE_REQUIREMENTS...