How to use boost::asio::io_service::run_one()

一世执手 提交于 2019-12-11 04:32:02

问题


I was reading up on boost::asio::io_service::run_one() and am confused by what it means by the function block. What has been blocked and where is the handler defined?


回答1:


I was reading up on boost::asio::io_service::run_one() and am confused by what it means by the function block. What has been blocked

Blocked means run_one() blocks until it completes one handler.

and where is the handler defined?

It isn't. Logically it's described in the documentation. A handler is whatever action is pending in the service. So, if you do:

 void foo() { /*.... */ }
 void bar() { /*.... */ }

 io_service svc;
 svc.post(foo);
 svc.post(bar);

Now the first time you call

 svc.run_one();

blocks until foo is completed. The second time

 svc.run_one();

will block until bar is completed. After that, run_one() will NOT block and just return 0. If you make the service stay around, e.g.:

 io_service::work keep_around(svc);
 svc.run_one();

would block until some other action was posted.



来源:https://stackoverflow.com/questions/44429978/how-to-use-boostasioio-servicerun-one

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!