使用strand::post 和io_service::post 与strand::wrap 到底有什么区别?
根据我的理解,将处理程序发布到 strand
对象意味着:
- 一次仅执行一个发布的处理程序。
- 处理程序按顺序调用。
将处理程序直接发布到 io_service
对象并通过 strand::wrap
包装它们也意味着一次仅执行一个发布的处理程序,但不按顺序执行。
还有其他区别吗?如何使用 strand
并行(在不同线程中)运行两个(或更多)不同类型的工作(因此,不同的处理程序/函数)?
In my understanding, posting handlers to a strand
object means:
- Only one of the posted handlers is executed at a time.
- The handlers are invoked in order.
Posting handlers directly to an io_service
object and wrapping them by strand::wrap
also means that only one of the posted handlers is executed at a time, but not in order.
Is there any other difference? And how can I run two (or more) different kind of works (so, different handlers/functions) parallel (in different threads) by using strand
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您希望它们并行运行,请不要使用支架。链用于序列化。只需发布到服务并让服务在线程池中运行。
但你提出了一个很好的观点,我希望有人能回答。到底是什么
不同之处?如果wrap 序列化所有处理程序,那么它们怎么会乱序,即看起来它与通过strand 进行发布的作用相同?你会在哪里使用其中一种而不是另一种?
If you want them to run in parallel don't use stands. Strands are for serializing. Just post to the service and have the service run in a thread pool.
But you bring up a good point that I would like someone to answer. What exactly is the
difference? If wrap serializes all handlers then how could they get out of order, i.e. seems like it does the same thing as posting through a strand? Where would you use one over the other?
事实上,
strand
是一个队列,因此通过wrap
将处理程序发布到io_service
与不换行相同,因为每次发布它时,您可以在不同的临时strand
中执行此操作(每个队列仅包含一个处理程序 - 所有这些处理程序都可以同时执行,因为它们不在同一strand
中)。如果您需要序列化处理程序,则它们必须由同一个strand
对象包装(因此该对象包含多个处理程序)。In fact,
strand
is a queue, so posting a handler toio_service
bywrap
is same as not to wrap because each time you post it, you do in a distinct tempstrand
(each queue contains only one handler -- all those handlers can be executed concurrently because they are not in the samestrand
). If you need serialized handlers, they must be wrapped by the samestrand
object (which therefore contains more than one handler).