What is the performance difference between strand.

2019-05-26 11:34发布

问题:

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.