Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between post and dispatch in boost::asio?

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 ?

like image 294
ANIKET ANVIT Avatar asked Jun 18 '18 14:06

ANIKET ANVIT


2 Answers

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.

like image 179
NathanOliver Avatar answered Oct 19 '22 05:10

NathanOliver


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.

like image 25
UKMonkey Avatar answered Oct 19 '22 03:10

UKMonkey