I am trying to use boost:asio library to create a threadpool. The official documentation says :
dispatch : Request the io_service to invoke the given handler.
post: Request the io_service to invoke the given handler and return immediately.
Could someone explain how these two differ ?
The difference is dispatch
may run handler
(the CompletionHandler
passed to it) inside it which means you will wait for it to finish, if it does, before the function returns. post
on the other hand will not run handler
itself and returns back to the call site immediately.
So, dispatch
is a potentially blocking call while post
is a non-blocking call.
Post
ensures that the thread that calls post will not immediately attempt to process the task.
https://www.boost.org/doc/libs/1_47_0/doc/html/boost_asio/reference/io_service/post.html
but without allowing the io_service to call the handler from inside this function.
Dispatch makes no such promise; and may be completed by the time the function returns.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With