- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试在多个线程中使用 asio::io_context。
#include <iostream>
#include <vector>
#include <asio.hpp>
#include <thread>
#include <future>
#include <functional>
int main()
{
asio::io_context ctx;
std::vector<std::future<asio::io_context::count_type>> tasks;
for(int i = 0; i < 3; ++i)
{
tasks.push_back(std::async(std::launch::async, std::bind(&asio::io_context::run, &ctx));
}
for(auto &task: tasks){tasks.get();}
return 0;
}
但是,我遇到了编译错误
asio_blah.cpp: In function ‘int main()’:
asio_blah.cpp:101:94: error: no matching function for call to ‘bind(<unresolved overloaded function type>, asio::io_context*)’
tasks.push_back(std::async(std::launch::async, std::bind(&asio::io_context::run, &ctx));
不确定为什么编译器无法识别成员函数指针(我相信成员函数类型是 asio::io_context::count_type (asio::io_context*)()
和函数签名应该对编译器可见,因为包含 asio.hpp)并报告错误 unresolved overloaded function type
。
有什么修复错误的建议吗?
最佳答案
你可以像这样使用 lambda:
#include <iostream>
#include <vector>
#include <boost/asio.hpp>
#include <thread>
#include <future>
#include <functional>
using namespace boost;
int main()
{
asio::io_context ctx;
std::vector<std::future<asio::io_context::count_type>> tasks;
for(int i = 0; i < 3; ++i)
{
tasks.push_back(std::async(std::launch::async, [&ctx]() {
return ctx.run();
}));
}
for(auto &task: tasks){task.get();}
return 0;
}
编辑:
正如 Miles Budnek 所说的那样, io_context::run 有多个重载。如果不通过强制转换强制进行重载解析,则无法获取指向它的指针。
如果您真的想使用 std::bind
,请进行转换。
我的看法和其他人一样。去 LAMBDA!!!
关于关于在不同线程中运行 io_context 的 C++ 编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53952914/
尝试实例化类的新对象时出现此错误。代码是: using boost::asio::ip::tcp; typedef boost::asio::io_service ioservice; class c
目前,我正在尝试将通过 post 或 dispatch 排队的工作移除到 io_context 中。工作由少量队列组排队,这些工作应立即全部移除: boost::asio::io_context co
由于最新版本的 boost,asio 推出了新的执行器并提供了 asio::strand .所以现在完全可以使用 asio::strand而不是 io_context::strand .但它们不能互换
我一直在使用 boost asio 库,其中大多数对象都需要 io_context 对象作为构造函数的参数。我已经阅读了 io_context 对象,根据文档,它指出它是 The io_context
在检查 1.66.0 版本中 boost::asio 的文档时,我注意到 io_context 构造函数提供了一个 concurrency_hint。范围。阅读文档后,我不确定是否可以使用 BOOST
我正在尝试在多个线程中使用 asio::io_context。 #include #include #include #include #include #include int main
我正在尝试使用 Boost Libraries 构建聊天室。但是当我尝试使用 asio::io_context 时,编译器说: io_context is not an member of asio.
我目前正在编写一个多线程服务器,其中每个线程都有一个 io_context 和一个要执行的任务对象列表,每个任务对象都有一个关联的 ip::tcp::socket 对象。 对于负载平衡,我有时会将任务
我有一个使用boost.beast实现的RESTServer.hpp,如下所示。 #pragma once #include #include #include #include #inclu
我正在使用 c++ 的 boost asio 库。我发现io_service和io_context有相似之处。例如,两者都有方法 run 和其他方法。有人可以详细说明这两个类之间的差异(如用法、概念思
此代码调用已发布的句柄 boost::asio::io_context ioc; boost::asio::post(ioc, []{ std::cout : boost::asio:
boost::asio::io_context::run() 确实在没有待处理的工作时返回。我想避免这种行为,这样 run() 就会无限期地等待新作品,并有可能从另一个线程停止它。 我想这可以通过在
我需要获得同步 I/O 但具有以下特性: 被其他线程中断 支持超时 因此,我使用来自 Boost.Asio 的异步 I/O 并通过 boost::asio::io_context::run_one_f
我在 中使用 boost::beast::websocket 和 boost::asio::io_context 编写了一个小型 websocket 客户端>C++。我有一个具有以下状态的状态机: e
据我所知,据我检查了 boost::asio 文档和源代码,除了销毁上下文本身之外,没有办法显式销毁给定 io_context 上所有挂起的处理程序吗? 如果可能的话,我需要能够停止 io_conte
我有 2 个 boost::asio::io_context 变量,一个用于我的 Raspberry Pi 和我的 arduino 之间的连接,另一个用于 Raspberry Pi 和客户端之间通过
我目前正在做一个使用 MQTT 协议(protocol)进行通信的项目。 专用文件中有一个 Session 类,它基本上只是设置发布处理程序,即当此客户端收到消息时调用的回调(处理程序检查主题是否匹配
我知道 epoll 和 io_context 是异步工作的。那么,你能告诉我这两者有什么区别吗? 你在 asio::io_context 中使用 epoll 吗? 最佳答案 POSIX 为我们提供了一
我的服务器基于boost spawn echo服务器示例,并在this thread中进行了改进。真实的服务器很复杂,我做了一个更简单的服务器来显示问题: 服务器监听端口12345,从新连接接收0x4
和 asio::thread_pool 有什么区别和一个 asio::io_context谁的run()函数是从多个线程调用的?我可以更换我的 boost::thread_group调用 io_con
我是一名优秀的程序员,十分优秀!