- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试为 boost::io_context
创建一个适配器,它将始终在准备好的处理程序中选择一个具有最高优先级的处理程序来执行。我的灵感来自 the official example ,但在一个场景中很快遇到意外行为,其中一个处理程序在同一上下文中启动另一个异步操作。
这是 MCVE .我只修改了用户代码(//---
下方),以调用低优先级处理程序,之后我希望调用高优先级和中优先级处理程序。仅调用低优先级处理程序。
#include <boost/asio.hpp>
#include <boost/function.hpp>
#include <iostream>
#include <queue>
class handler_priority_queue
{
public:
void add(int priority, boost::function<void()> function)
{
handlers_.push(queued_handler(priority, function));
}
void execute_all()
{
while (!handlers_.empty())
{
queued_handler handler = handlers_.top();
handler.execute();
handlers_.pop();
}
}
// A generic wrapper class for handlers to allow the invocation to be hooked.
template <typename Handler>
class wrapped_handler
{
public:
wrapped_handler(handler_priority_queue& q, int p, Handler h)
: queue_(q), priority_(p), handler_(h)
{
}
void operator()()
{
handler_();
}
template <typename Arg1>
void operator()(Arg1 arg1)
{
handler_(arg1);
}
template <typename Arg1, typename Arg2>
void operator()(Arg1 arg1, Arg2 arg2)
{
handler_(arg1, arg2);
}
//private:
handler_priority_queue& queue_;
int priority_;
Handler handler_;
};
template <typename Handler>
wrapped_handler<Handler> wrap(int priority, Handler handler)
{
return wrapped_handler<Handler>(*this, priority, handler);
}
private:
class queued_handler
{
public:
queued_handler(int p, boost::function<void()> f)
: priority_(p), function_(f)
{
}
void execute()
{
function_();
}
friend bool operator<(const queued_handler& a,
const queued_handler& b)
{
return a.priority_ < b.priority_;
}
private:
int priority_;
boost::function<void()> function_;
};
std::priority_queue<queued_handler> handlers_;
};
// Custom invocation hook for wrapped handlers.
template <typename Function, typename Handler>
void asio_handler_invoke(Function f,
handler_priority_queue::wrapped_handler<Handler>* h)
{
h->queue_.add(h->priority_, f);
}
//----------------------------------------------------------------------
void high_priority_handler()
{
std::cout << "High priority handler\n";
}
void middle_priority_handler()
{
std::cout << "Middle priority handler\n";
}
void low_priority_handler(
boost::asio::io_service& io_service,
handler_priority_queue& pri_queue)
{
std::cout << "Low priority handler\n";
io_service.post(pri_queue.wrap(1, middle_priority_handler));
io_service.post(pri_queue.wrap(2, high_priority_handler));
}
int main()
{
boost::asio::io_service io_service;
handler_priority_queue pri_queue;
// Post a completion handler to be run immediately.
io_service.post(pri_queue.wrap(
0, std::bind(low_priority_handler,
std::ref(io_service), std::ref(pri_queue))));
while (io_service.run_one())
{
// The custom invocation hook adds the handlers to the priority queue
// rather than executing them from within the poll_one() call.
while (io_service.poll_one())
;
pri_queue.execute_all();
}
return 0;
}
如果我在 main
中的循环之后调用 io_service.restart()
并在之后复制粘贴相同的循环,则将执行剩余的处理程序,并且在预期的命令。调试时,我可以看到处理程序仅在 asio_handler_invoke
中排队一次。
为什么 boost::io_context
在第一个处理程序之后停止运行?我要求的可能吗?
最佳答案
io_context
停止,因为在调用 poll_one
时没有任何准备好运行的处理程序。
[1] 第一个处理程序已发布:
io_service.post(pri_queue.wrap(0, std::bind(low_priority_handler,
std::ref(io_service), std::ref(pri_queue))));
[2] while (io_service.run_one())
等待直到有一个准备好运行的处理程序
[3] 在 run_one()
处理程序中被执行。您已经定义了 asio_handler_invoke()
,它提供了一些策略来调用处理程序的函数(主体)。默认策略只是调用函数,在您的情况下,函数对象排队进入 handler_priority_queue
,但 io_service 的
队列不接收要执行的处理程序。那么,当 low_priority_handler
的主体(通过调用 io_service.post
为 io_service
添加新的处理程序)被执行时?此函数从pri_queue.execute_all()
开始执行(在poll_one
之后调用),而不是在poll_one()调用期间
。 io_service.poll_one()
将 io_service
标记为在没有任何准备好运行的处理程序时停止。这是你的情况。您可以在 pri_queue.execute_all()
之后重置 io_service
,然后将调用所有处理程序。
关于c++ - Boost.Asio - 不执行所有处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54189894/
使用 asio 库,我想为 asio::serial_port 读/写调用使用超时。 是否可以使用相同的 asio::serial_port asio::io_context 和用于 asio 的相同
对于我正在从事的副业项目应该使用哪种类型的解析器,我有点困惑。我在 asio 文档中找不到答案。 我知道 DNS 可以与 UDP 或 TCP 一起使用,并且通常通过 TCP 发送较大的响应。 asio
在仅从一个线程调用 io_service::run() 的情况下,从不同线程调用 async_write 和 async_read 是否安全?谢谢! 最佳答案 Is it safe to call a
我想知道Boost ASIO 有多受欢迎。它是否被用于任何流行的网络密集型软件中? 最佳答案 用于管理 IBM Blue Gene/Q 的系统软件 super 计算机广泛使用Boost.Asio。
我想使用一个函数来读取套接字端口,并在收到 IP 数据包时交还控制权。 boost::asio::ip::udp::socket 有一个函数接收(或 async_receive),它返回读取了多少字节
我试图调整 Boost 文档中的 SSL 服务器示例 here但我想制作一个应用程序,您可以在其中使用普通 boost::asio::ip::tcp::socket或 SSL 套接字,但我还没有找到将
在查看 boost asio co_spawn 文档 ( https://www.boost.org/doc/libs/1_78_0/doc/html/boost_asio/reference/co_
我正在尝试使用 Boost.ASIO 库,但我找不到如何列出 boost 的可用端口(带有串行端口服务)或套接字(带有网络服务)。 你知道这是否可能吗? 谢谢你。 最佳答案 Boost.Asio 不提
我想使用boost::asio从多个stdout中同时读取stderr和boost::process。但是,我在使用boost::asio时遇到了编译问题,可以重建以下无法编译的最小示例: #incl
提前为一个愚蠢的问题道歉 - 我对这一切都很陌生。 所以我从 here 下载了 asio ,并尝试#include asio.hpp,但出现以下错误; fatal error: boost/confi
我是使用 boost 的项目的一部分作为一个 C++ 库。现在我们要使用 SMTP/POP3/SSL/HTTP/HTTPS。我在 Poco::Net 中检测到几个拟合类和函数 Poco::Net::P
有谁知道有任何实现 Web Sockets 的尝试吗?使用 Boost asio 的 API? 最佳答案 我意识到这是一个旧线程,但想更新以帮助那些寻找答案的人:WebSocket++完全符合要求。
和 asio::thread_pool 有什么区别和一个 asio::io_context谁的run()函数是从多个线程调用的?我可以更换我的 boost::thread_group调用 io_con
我想连接到由目标 IP 地址和端口号指定的服务器套接字。 boost::asio::connect 似乎不允许使用它。我有 ip 目的地作为无符号 int 值。 更新:我能够做到 ba::ip::tc
我在 pc 上有 3 个网络接口(interface),并且想确保当我进行 udp 套接字发送时,它通过特定的网络接口(interface)发送(我有发送数据时使用的 ip 地址)。 这是代码。 ud
我正在使用 ASIO 开发网络应用程序并提到了Chat-Server/Client 我问过类似的问题Here 为了更好地解释,我在这里添加了更多代码: 我的 Cserver Class class C
我已经阅读了 boost asio 引用资料,浏览了教程并查看了一些示例。尽管如此,我还是看不出应该如何拆除套接字: 我应该调用 close() 还是由套接字的析构函数完成? 什么时候应该调用 shu
我认为标题已经说明了大部分内容,但我也有兴趣了解在没有现有解决方案的情况下如何将 DTLS 支持引入 asio 最佳答案 ASIO 本身不支持DTLS 但有一个GitHub 库asio_dtls已向
我正在将 async_read 与 streambuf 一起使用。但是,我想将读取的数据量限制为 4,这样我就可以在进入正文之前正确处理 header 。 我如何使用 async_read 做到这一点
从this example开始,我想用 async_read_until() 替换 async_read()。 所以我查了一下this example ,并查看了如何调用 async_read_unt
我是一名优秀的程序员,十分优秀!