Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dedicated thread for io_service::run()

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.

like image 965
deft_code Avatar asked Dec 21 '09 20:12

deft_code


1 Answers

You need to create an io_service::work object.

See this section of the documentation:

Stopping the io_service from running out of work

like image 97
janm Avatar answered Oct 21 '22 00:10

janm