boost::io_service::post 线程安全吗?
从处理程序中发布新处理程序是线程安全的吗? 即调用 io_service::run() 的线程可以将新的处理程序发布到同一个 io_service 吗?
谢谢
Is it thread safe to post new handlers from within a handler?
I.e. Can threads that called the io_service::run()
post new Handlers to the same io_service?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
安全根据文档,从 io_service 的单个实例的处理程序中发布处理程序。
It is safe to post handlers from within a handler for a single instance of an
io_service
according to the documentation.我对这篇文章进行了大量编辑,请注意:原始代码有一些细微的错误,因此它似乎反驳了 ASIO 的线程安全性。我已经标记了执行后获得正确计数器 3000000 所需的更正。查看编辑历史记录以查看所做的代码更改。最重要的更新是将
service.stop()
更改为guard.reset(),这允许 io_service
退出,一旦所有工作完成。我选择编辑而不是发布不同的答案,因为大部分代码仍然来自原作者。由于后者在很多很多年前才活跃,而且关于他的错误的评论也很早以前就已经发表了,所以等待更长时间是没有意义的。
Note by me, who edited this post heavily: The original code had a few subtile bugs, so that it seemed to disproof thread-safety of ASIO. I have marked the corrections necessary to get the correct counter of 3000000 after execution. Look at the edit history to see the code changes made. The most important update is to change
service.stop()
, which keeps some of the posted work undone, toguard.reset()
, which allows theio_service
to exit, once all work has been completed.I choose to edit instead of post a different answer, as most of the code is still from the original author. As the later was only active many, many years ago and comments about his bugs had been made long time ago, it wouldn't have made sense to wait longer.