I’m studying multithreading, and I came across that it is preferable to use async() with io_context . But I don’t quite understand why they are needed. I re-read several documentations and watched many lectures on this topic, but I still couldn’t figure out what the difference, for example, in this code:
boost::asio::io_context io;
io.post([](){
do_some_work()
});
std::thread thread([&io]() {
io.run();
});
from this:
std::thread thread([&io]() {
do_some_work();
});
I also saw many examples of using io_context with async in client-server interaction with so-called handlers. But I just don’t understand their meaning. Why are they needed if you can just write methods in a row:
socket();
connect();
read();
handle();
Why use a complex async construction with io_context, if you can write everything structurally (as I did above) using future/promise and it will be readable and understandable?