【问题标题】:How to bind parameter with Boost asio io_context post?如何将参数与Boost asio io_context post绑定?
【发布时间】:2020-09-12 14:57:19
【问题描述】:

我正在尝试将数据发布到 io_context,如果我将 post 与 newdata 方法(不带参数)绑定,它会被调用,但是如果我传递任何参数,它无法调用 newdata 方法。

std::shared_ptr< boost::asio::io_context >ioc = std::make_shared<boost::asio::io_context>();
 :
ioc->post(boost::bind(&myclass::newdata, this));  /// <-- this get called
ioc->post(boost::bind(&myclass::newdata, this, 1));  /// <-- this get failed, no compile time error

//我尝试了boost::function,但编译失败

boost::function<void(int>)> p(boost::bind(&myclass::newdata, this, _1));
ioc->post(p(1));   // <-- compile time error

【问题讨论】:

  • 没有特定于 boost 和 asio 的东西,使用现代 C++ lambda 函数而不是旧的绑定
  • @JeanDavy 我尝试使用 lambda,但我观察到它也失败了几次,不知道出了什么问题。
  • 所以关注问题所在,问题不在于 lambda 本身。
  • 我发现该帖子已成功运行,但处理程序从未被执行。不知何故,ioc 没有执行处理程序......更多地研究它。

标签: c++ boost boost-asio boost-bind boost-beast


【解决方案1】:

调用 ioc->post() 的线程没有 ioc->run(),只要我把它设置为它就可以了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-22
    • 1970-01-01
    • 2019-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-02
    • 2020-06-17
    相关资源
    最近更新 更多