views:

363

answers:

1

I want to provide a global io_service that is driven by one global thread. Simple enough, I just have the thread body call io_service::run(). However, that doesn't work as run (run_one, poll, poll_one) return if there is no work to do. But, if the thread repeatedly calls run(), it will busy loop when there is nothing to do.

I'm looking for a way to get the thread to block while there isn't any work to be done in the io_service. I could add a global event to the mix for the thread to block on. However, that would require users of the io_service to notify the event every time they used the service. Not the ideal solution.

Note: there are no actual globals and I never use events for concurrency I just simplified the problem down to my exact need. The real goal is a asio::deadline_timer subclass that doesn't require an io_service as a construction parameter.

+6  A: 

You need to create an ioservice::work object.

See this section of the documentation:

Stopping the io_service from running out of work

janm
Did not know about this one. +1, and deleted my hack-ishy suggestion.
Éric Malenfant
thanks, I had started to make timer with and infinity timeout just for this purpose. I am perpetually impressed with asio. They missed very few corner cases.
caspin