Reputation: 3049
I'd like for someone to tell me the pros (and cons) for using the post function. Why and when should I prefer using post and why/when should I not want to use it?
Upvotes: 9
Views: 3895
Reputation: 13993
I'm guessing you're comparing post()
to dispatch()
. In general, post()
is safer, because dispatch()
may call the handler right away, and there is a risk in event-driven programming that you affect queued events in unexpected ways. Also there is merit in keeping the call stack small, and in having more predictable behavior (dispatch()
may run now or may run later).
I just found post()
useful for deleting an object only after the current io_service
event/handler/operation finishes.
Upvotes: 1
Reputation: 18339
Post is very useful when you want the callback to occur essentially now, but not in the current context. Reasons might include:
The current context is holding locks and you want the function to be called after they have been released. This would allow the function to acquire those locks itself without causing a deadlock.
The call stack might be very deep
The current thread might be inappropriate for the function in some other way, and post is a convenient way of scheduling the function in another thread.
Upvotes: 10