- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我想在最后一个异步事件发生后延迟 n 秒执行一次特定操作。因此,如果连续事件在不到 n 秒内出现,则特定操作将延迟(重新启动 deadline_timer)。
我从 boost deadline_timer issue 改编了计时器类为了简单起见,事件是同步生成的。运行代码,我期待这样的事情:
1 second(s)
2 second(s)
3 second(s)
4 second(s)
5 second(s)
action <--- it should appear after 10 seconds after the last event
但是我明白了
1 second(s)
2 second(s)
action
3 second(s)
action
4 second(s)
action
5 second(s)
action
action
为什么会这样?如何解决?
#include <boost/asio.hpp>
#include <boost/thread.hpp>
#include <iostream>
class DelayedAction
{
public:
DelayedAction():
work( service),
thread( boost::bind( &DelayedAction::run, this)),
timer( service)
{}
~DelayedAction()
{
thread.join();
}
void startAfter( const size_t delay)
{
timer.cancel();
timer.expires_from_now( boost::posix_time::seconds( delay));
timer.async_wait( boost::bind( &DelayedAction::action, this));
}
private:
void run()
{
service.run();
}
void action()
{
std::cout << "action" << std::endl;
}
boost::asio::io_service service;
boost::asio::io_service::work work;
boost::thread thread;
boost::asio::deadline_timer timer;
};
int main()
{
DelayedAction d;
for( int i = 1; i < 6; ++i)
{
Sleep( 1000);
std::cout << i << " second(s)\n";
d.startAfter( 10);
}
}
PS 写这篇文章时,我认为真正的问题是 boost::deadline_timer 启动后如何重新启动。
最佳答案
当您调用 expires_from_now()
时,它会重置计时器,并立即使用错误代码调用处理程序 boost::asio::error::operation_aborted
。
如果您在处理程序中处理错误代码情况,则可以按预期进行。
void startAfter( const size_t delay)
{
// no need to explicitly cancel
// timer.cancel();
timer.expires_from_now( boost::posix_time::seconds( delay));
timer.async_wait( boost::bind( &DelayedAction::action, this, boost::asio::placeholders::error));
}
// ...
void action(const boost::system::error_code& e)
{
if(e != boost::asio::error::operation_aborted)
std::cout << "action" << std::endl;
}
文档中对此进行了讨论: http://www.boost.org/doc/libs/1_47_0/doc/html/boost_asio/reference/deadline_timer.html
具体请参阅标题为:更改事件的 deadline_timer 的到期时间的部分
关于c++ - 使用 boost::deadline_timer 延迟 Action ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11878091/
我正在研究基于 boost 1.52 的 boost::asio::deadline_timer 的实现。 如 the article on highscore 中所述,它分三部分实现: deadli
我需要在两个线程之间共享一个 boost::deadline_timer。 boost 文档说“共享实例不是线程安全的”。这是一个示例代码: ClassA : public enable_shared
我有点迷失在库的构造中,我必须将它们纠缠在一起。我需要帮助将一些计时器引入到这个结构中。 我有以下内容: com.cpp 具有 main 并包含 com.hpp com.hpp 包含一个 host.h
我已经为 boost::asio 实现了一个非常标准的阻塞 api 模拟和超时。这是我的主要周期: io_service io_svc; tcp::endpoint endpoint(tcp::v4(
我有一个这样的测试类。我想要做的是继续运行这个对象中的三个计时器。但是在我实例化一个对象之后,一些计时器会一直重复,但其他计时器会在 3 分钟后消失。谁能给我解释一下? class EventProc
调用 deadline_timer::wait 是否会导致 io_service 中的其他任务在等待时执行,或者如果在其中调用它是否会完全阻塞 io 线程? io_service service; i
我正在使用一个 boost deadline_timer,它似乎在拥有的对象被删除后调用它的处理程序。我已经尝试了几种方法来让它发挥作用。 首先,我只是通过绑定(bind)到处理程序和使用“share
在阅读了 boost::asio::deadline_timer 的文档之后,似乎 io_service::run() 和处理程序方法是在同一个线程上调用的。有什么方法可以在一个线程上创建一个计时器,
我很惊讶没有在 boost::asio(我们任何广泛使用的库)中找到时钟组件,所以它尝试制作一个简单、简约的实现来测试我的一些代码。 使用 boost::asio::deadline_timer 我制
当您在运行的计时器上调用 expires_from_now() 时,定时器被取消,并调用一个新的定时器。因此调用关联的处理程序。在处理程序中很容易区分在已取消和已过期的计时器之间。然而,我想知道,是否
我希望下面的代码打印 Hello, world!每 5 秒,但发生的情况是程序暂停 5 秒,然后一遍又一遍地打印消息,没有后续暂停。我错过了什么? #include #include #inclu
下面是一个用计时器包装线程的测试类的实现。奇怪的是,如果将截止日期设置为 500 毫秒,它会起作用,但如果我将它设置为 1000 毫秒,它就不会起作用。我做错了什么? #include "TestTi
我尝试在这个简单的测试应用程序中使用 boost deadline_timer,但遇到了一些麻烦。目标是使用 deadline_timer 的 expires_at() 成员函数让计时器每 45 毫秒
我正在使用多个 boost::asio::deadline_timer在一个 io_service 对象上。 std::shared_ptr的 boost::asio::deadline_timer存
我正在学习 Boost.Asio,但我对 boost::asio::deadline_timer async_wai 有疑问。这是来自 boost 主页的代码: // // timer.cpp //
我有一个 tcp 客户端,它会在截止日期前轮询服务器以获得答案,这样如果无法访问服务器,客户端就不会被阻止。我遇到的问题是 async_wait 似乎从不调用它的处理程序,在连接失败时有效地阻塞了客户
我能够为 boost deadline_time(它是一个成员)创建一个处理程序通过声明它是静态的。不幸的是,这会阻止访问非静态成员数据。 我有一系列超时。所以我的想法是有一个单一的 deadline
我使用 boost::asio::deadline_timer 来运行一个函数。我有如下 MosquitoInterface 类 class MosquitoInterface{ Mosquit
我想知道 boost::asio::deadline_timer 线程安全吗? 有人可以回答我吗? 最佳答案 您要查找的信息可以在documentation中找到. Thread Safety Dis
这个问题在这里已经有了答案: boost asio deadline_timer async_wait(N seconds) twice within N seconds cause operati
我是一名优秀的程序员,十分优秀!