相关疑难解决方法(0)

asio 如何更改等待中的执行者?

我已阅读此问题并尝试使用以下代码复制答案:

#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)

c++ boost boost-asio c++20 c++-coroutine

8
推荐指数
1
解决办法
1493
查看次数

标签 统计

boost ×1

boost-asio ×1

c++ ×1

c++-coroutine ×1

c++20 ×1