What is the difference between post and dispatch from boost::asio when overloads with executor parameter are used?

huangapple go评论72阅读模式
英文:

What is the difference between post and dispatch from boost::asio when overloads with executor parameter are used?

问题

我看到了一些类似的问题,涉及到在仅传递处理程序时,关于postdispatch的区别,这些处理程序是在关联的执行器上发布的。我理解当当前执行在关联的执行器上执行时,dispatch可以调用处理程序的同步调用而不将其排队,但我认为同样的原则也适用于第二个重载,但在文档中明确说明:“函数对象被排队等待执行,并且在从dispatch()返回之前从当前线程中不会调用它”。

那么,在这种情况下,postdispatch有什么区别?

英文:

I saw several questions like this, regarding post vs dispatch when only handler is passed which is posted on associated executor. I understand that dispatch can invoke synchronous call of handler without queueing it, when current execution is performed on the associated executor. I thought the same principle would be applied to second overload, but it is specifically said in the documentation that The function object is queued for execution, and is never called from the current thread prior to returning from dispatch().

So what is the difference between post and dispatch in this case?

答案1

得分: 1

你链接到了旧的文档。

此外,即使在那个版本中,关于非执行器重载的文档也said the exact same thing,因此所谓的区别并不存在。

我认为这是文档中的一个疏忽,即使在那时,current versions也明确指出处理程序可能在返回之前被调用:

可能会从当前线程中调用函数对象,然后才从dispatch()返回。

旁注

我想注意一下,即使在从dispatch返回之前没有被调用,调用仍然可以进行优化,因为

  • 处理程序可能会被推送到本地队列中,并在从当前正在执行的处理程序返回后立即执行。这实际上可能对分配/释放顺序模式更
  • 处理程序可能具有用于立即执行的关联执行器。这是一个新功能。

¹ 修复在commit 0340c15e4 Fix up dispatch() documentation. Clarify distinction between post() and defer().中进行(2019年3月7日)。

英文:

You're linking to old documentation.

Further, even in that version, the documentation for the non-executor overload said the exact same thing, so the purported difference didn't exist.

I think it was an oversight¹ in documentation, even then, and current versions make it clear that the handler may be invoked prior to return:

> The function object may be called from the current thread prior to returning from dispatch()

Side Note

I'd like to note that invocation can still be optimized even if not invoked before returning from dispatch itself, because

  • the handler maybe pushed to a local queue instead and be executed immediately after returning from a currently executing handler. This may actually be better for allocation/deallocation ordering patterns.
  • the handler may have an associated executor for immediate execution. This is a new feature

¹ the fixup was in the commit 0340c15e4 Fix up dispatch() documentation. Clarify distinction between post() and defer(). (Mar 7 2019(

huangapple
  • 本文由 发表于 2023年6月29日 22:00:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76581761.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定