The output is:
Handler A
Handler B
Handler D
Handler E
Handler C
Given,
-
post() puts the handler into the taskqueue and returns immediately
-
dispatch() can run the task immediately if the main thread already calls run() (which is the case)
then,
-
why "Handler E" wasn't run before B and D ? It was dispatch(), and main thread already runs the io_context after all.
-
why "Handler C" was run last ? It kinds of make sense as it was post within post. But still the order of the tasks being put to the taskqueue isn't very self-explained.
int main()
{
boost::asio::io_service io_service;
io_service.dispatch( [](){ cout << "Handler A\n"; } );
io_service.post(
[&io_service]() {
cout << "Handler B\n";
io_service.post(
[](){
cout << "Handler C\n";
}
);
}
);
io_service.post( []() {
cout << "Handler D\n";
}
);
io_service.dispatch( [](){ cout << "Handler E\n"; } );
cout << "Running io_service\n";
io_service.run();
return 0;
}
Aucun commentaire:
Enregistrer un commentaire