What is the performance difference between strand::post()
and strand::wrap()
? And what's the story about race condition when use strand::wrap(
)?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
Just for clarification, strand::wrap
only creates a handler, neither the provided handler nor returned handler are dispatched or posted to the io_service
. If the result from strand::wrap
is executed, then performance differences from overhead is negligible. However, a perceived performance difference could occur as the result of the wrapped handler using dispatch()
, where under certain conditions the user's handler will be executed immediately, instead of post()
, where the user's handler is always added to the queue.
Also, there should be no race conditions when using a strand. Per the documentation, it is thread safe for distinct and shared objects.