Short answer: no, you do not need to use strand in this case.
Widely simplified, io_service contains a list of function objects (handlers). Handlers are put on the list when post() is called in the service. for example, whenever an asynchronous operation completes, the handler and its arguments are put into a list. io_service::run() executes one handler after another. Therefore, if there is only one thread calling run() , as in your case, there is no synchronization problem, and there is no strand .
Only if several threads call run() on the same io_service , several handlers will be executed at the same time, in N threads - up to N simultaneous handlers. If this is a problem, for example, if there can be two handlers in the queue at the same time as accessing the same object, you will need strand .
You can see strand as a kind of lock for a group of handlers. If the thread runs the handler associated with strand , then strand blocked, and it is freed after the handler is executed. Any other thread can only execute handlers that are not associated with a locked strand .
Caution: this explanation may be too simplistic and technically inaccurate, but it gives a basic idea of ββwhat happens in io_service and strand s.
Arne mertz
source share