|
Boost Users : |
From: Yuval Ronen (ronen_yuval_at_[hidden])
Date: 2007-07-24 17:47:46
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
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net