Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does `boost::asio::deadline_timer` allocate a `timer_queue` instance for each `deadline_timer` object?

I am looking into the implementation of boost::asio::deadline_timer, based on boost 1.52.

As described in the article on highscore, it is implemented in three parts:

  • deadline_timer, the i/o object
  • deadline_timer_service, the service
  • detail::deadline_timer_service, the service implementation

There will be a single instance of deadline_timer_service serving many deadline_timer objects. An instance of detail::deadline_timer_service is created for each deadline_timer object. Moreover, the timers are scheduled by a scheduler, which is usually a reactor (i.e. epoll_reactor on Linux in most cases).

There is a timer_queue_ member of type timer_queue in detail::deadline_timer_service which is a heap that stores scheduled timers. This means that there will be one instance of timer_queue for each deadline_timer object. Multiple timer_queue objects can be added to the scheduler by add_timer_queue(), and they are managed by a class timer_queue_set.

My question is, why asio create multiple queues to handle timers? What is the drawback if we have a single timer queue instance in the scheduler that all timers can share?

like image 604
Danqi Wang Avatar asked Mar 04 '26 08:03

Danqi Wang


1 Answers

I read the code again and find my mistake. The actual relationship is as follows:

  • basic_deadline_timer is the i/o object type, one instance for each deadline_timer;
  • deadline_timer_service is the service type, one per io_service, retrieved by the constructor of basic_io_object;
  • detail::deadline_timer_service is the service implementation type, one per service, hen one per io_serive as well. It is defined as the data member of deadline_timer_service;
  • detail::deadline_timer_service::implementation_type: the implementation type of the timer, one per deadline_timer.

As timer_queue_ is a member of detail::deadline_timer_service, it is one per io_service as well. Looks reasonable for me now.

The highscore link seems have merged the service implementation type and timer implementation type into one, which is a bit misleading.

like image 103
Danqi Wang Avatar answered Mar 07 '26 00:03

Danqi Wang



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!