[asio] best strategy for creating lots of timers

Hi. I'm writing an application that uses many timers in many places. I'd like to use asio::deadline_timer for this purpose, and an asio::deadline_timer requires an instance of asio::io_service to function. The easy way to do it is to create an asio::io_service along side with each instance of deadline_timer. However, I fear that this is may be wasteful in terms of OS resources. So my first question is whether this is true. If that's true, then I'm not creating an io_service for each deadline_timer, so I must be sharing a single io_service by several timers. The problem with such a solution is that the io_service doesn't know about the timers using it, and especially, it doesn't know how many such timer there are at any given point in time, and if there are any. If the number of timers is zero, then there is no work to be done by the io_service, and the thread calling io_service::run() will be released. This is bad, because when a new timer will be created, it will require that there will be a thread calling io_service::run(). So what's the best solution? One possible way is to accompany each io_service with a dummy deadline_timer that will never expire (expires_from_now(infinity)). This will cause the io_service to not release its thread, until that dummy timer is cancel()ed. Another way is to wrap the call to io_service::run() with a loop that checks a whether it should call run() or wait until some new timer is added (using a boost::condition). There might be other options in addition to these two. What do you think? Yuval

Hey Yuval,
On 24/07/07, Yuval Ronen
Hi. I'm writing an application that uses many timers in many places. I'd like to use asio::deadline_timer for this purpose, and an asio::deadline_timer requires an instance of asio::io_service to function. The easy way to do it is to create an asio::io_service along side with each instance of deadline_timer. However, I fear that this is may be wasteful in terms of OS resources. So my first question is whether this is true.
I think you're right. Having an io_service-per-timer would probably be quite wasteful. If that's true, then I'm not creating an io_service for each
deadline_timer, so I must be sharing a single io_service by several timers. The problem with such a solution is that the io_service doesn't know about the timers using it, and especially, it doesn't know how many such timer there are at any given point in time, and if there are any. If the number of timers is zero, then there is no work to be done by the io_service, and the thread calling io_service::run() will be released. This is bad, because when a new timer will be created, it will require that there will be a thread calling io_service::run(). So what's the best solution?
You can always just give the io_service some work to do by constructing an io_service::work object with the io_service. If you control the lifetime of the work object then you control the io_service and there's no need for a dummy timer. There's also a good example in the asio docs (the HTTP server 2, IIRC, here: *http://tinyurl.com/2w5k4r*) that might be helpful (see the io_service_pool.*pp files in particular). HTH, Darren

Darren Garvey wrote:
Hey Yuval,
On 24/07/07, *Yuval Ronen*
mailto:ronen_yuval@yahoo.com> wrote: Hi. I'm writing an application that uses many timers in many places. I'd like to use asio::deadline_timer for this purpose, and an asio::deadline_timer requires an instance of asio::io_service to function. The easy way to do it is to create an asio::io_service along side with each instance of deadline_timer. However, I fear that this is may be wasteful in terms of OS resources. So my first question is whether this is true.
I think you're right. Having an io_service-per-timer would probably be quite wasteful.
If that's true, then I'm not creating an io_service for each deadline_timer, so I must be sharing a single io_service by several timers. The problem with such a solution is that the io_service doesn't know about the timers using it, and especially, it doesn't know how many such timer there are at any given point in time, and if there are any. If the number of timers is zero, then there is no work to be done by the io_service, and the thread calling io_service::run() will be released. This is bad, because when a new timer will be created, it will require that there will be a thread calling io_service::run(). So what's the best solution?
You can always just give the io_service some work to do by constructing an io_service::work object with the io_service. If you control the lifetime of the work object then you control the io_service and there's no need for a dummy timer. There's also a good example in the asio docs (the HTTP server 2, IIRC, here: *http://tinyurl.com/2w5k4r*) that might be helpful (see the io_service_pool.*pp files in particular).
Thanks!
participants (2)
-
Darren Garvey
-
Yuval Ronen