我已阅读此问题并尝试使用以下代码复制答案:
#include <iostream>
#include <syncstream>
#include <thread>
#include <coroutine>
#include <boost/asio.hpp>
#include <boost/asio/experimental/as_single.hpp>
#include <boost/bind/bind.hpp>
#include <boost/thread/thread.hpp>
inline std::osyncstream tout() {
auto hash = std::hash<std::thread::id>{}(std::this_thread::get_id());
return std::osyncstream(std::cout) << "T" << hash << " ";
}
namespace asio = boost::asio;
asio::awaitable<void> mainCo(asio::io_context &appIO, asio::io_context &prodIO) {
// the thread should also change when using the IO contexts directly.
auto astrand = asio::io_context::strand{appIO};
auto pstrand = asio::io_context::strand{prodIO};
tout() << "MC on APPIO" << std::endl;
co_await asio::post(pstrand, asio::use_awaitable);
tout() << …
Run Code Online (Sandbox Code Playgroud)