io_service :: run()的专用线程 [英] dedicated thread for io_service::run()
问题描述
我想提供一个由一个全局线程驱动的全局io_service
.很简单,我只有线程主体调用io_service::run()
.但是,如果没有工作要做,则这不起作用,因为run
(run_one
,poll
,poll_one
)返回.但是,如果线程重复调用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.
我正在寻找一种方法来使线程阻塞,而io_service中没有任何工作要做.我可以将全局事件添加到混合中,以阻止线程继续运行.但是,这将要求io_service
的用户在每次使用该服务时通知事件.不是理想的解决方案.
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.
注意:没有实际的全局变量,并且我从不使用事件进行并发,我只是根据我的实际需要将问题简化了.真正的目标是asio::deadline_timer
子类,它不需要io_service
作为构造参数.
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.
推荐答案
您需要创建一个io_service::work
对象.
请参阅文档的此部分:
这篇关于io_service :: run()的专用线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!