- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个这样的测试类。我想要做的是继续运行这个对象中的三个计时器。但是在我实例化一个对象之后,一些计时器会一直重复,但其他计时器会在 3 分钟后消失。谁能给我解释一下?
class EventProcessor
{
private:
boost::asio::deadline_timer* m_Timer0;
boost::asio::deadline_timer* m_Timer1;
boost::asio::deadline_timer* m_Timer2;
boost::asio::io_service io0;
boost::asio::io_service io1;
boost::asio::io_service io2;
int TimerInterval[3];
boost::asio::deadline_timer* Timers[3];
public:
EventProcessor(int p_0, int p_1, int p_2)
{
TimerInterval[0] = p_0;
TimerInterval[1] = p_1;
TimerInterval[2] = p_2;
m_Timer0= new boost::asio::deadline_timer(io0, boost::posix_time::seconds(TimerInterval[0]));
Timers[0] = m_Timer0;
m_Timer1 = new boost::asio::deadline_timer(io1, boost::posix_time::seconds(TimerInterval[1]));
Timers[1] = m_Timer1;
m_Timer2 = new boost::asio::deadline_timer(io2, boost::posix_time::seconds(TimerInterval[2]));
Timers[2] = m_Timer2;
m_Timer0->async_wait(boost::bind(&EventProcessor::HandleExpire, this, boost::asio::placeholders::error, 0));
m_Timer1->async_wait(boost::bind(&EventProcessor::HandleExpire, this, boost::asio::placeholders::error, 1));
m_Timer2->async_wait(boost::bind(&EventProcessor::HandleExpire, this, boost::asio::placeholders::error, 2));
StartWithNewThread(0);
StartWithNewThread(1);
StartWithNewThread(2);
}
private:
void HandleExpire(const boost::system::error_code& p_ec, int p_TimerIndex)
{
if(p_ec == boost::asio::error::operation_aborted)
{
std::cout << "Timer" << p_TimerIndex << " canceled" << std::endl;
return;
}
std::cout << "Timer" << p_TimerIndex << " expired" << std::endl;
//Reset(p_OriginalTimer, TimerInterval[p_TimerIndex], p_TimerIndex);
boost::thread Thread(boost::bind(&EventProcessor::Reset, this, p_TimerIndex, TimerInterval[p_TimerIndex]));
}
void Start(int p_Index)
{
boost::asio::io_service& UnderlyingIO = Timers[p_Index]->get_io_service();
UnderlyingIO.reset();
UnderlyingIO.run();
UnderlyingIO.stop();
return;
}
void StartWithNewThread(int p_Index)
{
boost::thread Thread(boost::bind(&EventProcessor::Start, this, p_Index));
std::cout << Thread.get_id() << "<->" << "Timer" << p_Index << std::endl;
return;
}
public:
void Reset(int p_Index, int p_Seconds)
{
Timers[p_Index]->cancel();
Timers[p_Index]->expires_from_now(boost::posix_time::time_duration(0,0,p_Seconds,0));
TimerInterval[p_Index] = p_Seconds;
Timers[p_Index]->async_wait(boost::bind(&EventProcessor::HandleExpire, this, boost::asio::placeholders::error, p_Index));
boost::asio::io_service& UnderlyingIO = Timers[p_Index]->get_io_service();
UnderlyingIO.reset();
UnderlyingIO.run();
UnderlyingIO.stop();
return;
}
};
最佳答案
所以你应该这样做:
#include "test.h"
#include <boost/asio.hpp>
#include <boost/thread.hpp>
#include <boost/atomic.hpp>
class EventProcessor
{
private:
std::unique_ptr<boost::asio::deadline_timer> m_Timers[3];
boost::asio::io_service service;
boost::atomic<int> TimerInterval[3];
public:
EventProcessor(int time0,int time1, int time2)
{
TimerInterval[0] = time0;
TimerInterval[1] = time1;
TimerInterval[2] = time2;
for (int i = 0; i < 3; i++)
{
m_Timers[i].reset(
new boost::asio::deadline_timer(service));
}
}
~EventProcessor()
{
service.stop();
for (int i = 0; i < 3; i++)
{
m_Timers[i]->cancel();
}
}
void Run()
{
for (int i = 0; i < 3; i++)
{
m_Timers[i]->expires_from_now(boost::posix_time::seconds(TimerInterval[i]));
m_Timers[i]->async_wait(boost::bind(&EventProcessor::HandleExpire,
this,
i,
_1));
}
service.run();
}
void RunAsync()
{
boost::thread(boost::bind(&EventProcessor::Run,this));
}
void Reset(int i,int seconds)
{
TimerInterval[i] = seconds;
m_Timers[i]->expires_from_now(boost::posix_time::seconds(TimerInterval[i]));
m_Timers[i]->async_wait(boost::bind(&EventProcessor::HandleExpire,
this,
i,
_1));
}
private:
void HandleExpire(int p_TimerIndex, const boost::system::error_code& error)
{
if(error == boost::asio::error::operation_aborted)
{
std::cout << "Timer" << p_TimerIndex << " canceled" << std::endl;
return;
}
std::cout << "Timer" << p_TimerIndex << " expired" << std::endl;
//Reset(p_OriginalTimer, TimerInterval[p_TimerIndex], p_TimerIndex);
m_Timers[p_TimerIndex]->expires_from_now(
boost::posix_time::seconds(TimerInterval[p_TimerIndex]));
m_Timers[p_TimerIndex]->async_wait(boost::bind(&EventProcessor::HandleExpire,
this,
p_TimerIndex,
_1));
}
};
int main()
{
EventProcessor ev(1,2,3);
ev.RunAsync();
getchar();
ev.Reset(2,4);
getchar();
}
当然,我没有任何花哨的检查器来查看您当前是否正在运行(如果您希望安全使用,则完全需要)。
您可以将 boost::asio::io_service 视为可以进行异步调用的上下文。它创建一个 FIFO 消息队列来处理,并在您指定的时间和地点处理它们。处理这些消息最常见的方法是 boost::asio::io_service::run,它将处理消息直到没有什么可做的。 “无事可做”是一个灵活的定义:它不一定意味着有消息要处理,只是有事情要做。只要 async_wait 在调用处理程序之前,截止日期计时器之类的东西就可以确保“有事情要做”。您可以通过创建 boost::asio::io_service::work 实例手动强制执行某些操作。这使得在工作对象的生命周期内“还有事情要做”。
截止日期计时器类会为您处理所有异步调用,因此您不必生成所有这些线程。 io_service 执行同步,这是防止烦人的控制问题所必需的。
所以关于你的代码的问题:
由于所有这些线程都在控制 io_service,所以很难说出到底出了什么问题……我不得不猜测可能出了什么问题。我会把钱放在某个地方,你在截止日期计时器超时之前调用 io_service::cancel,这将停止你的循环。我在我的代码中通过在一个同步线程(io_service::run 调用)中执行所有控制(调用 wait_async)并仅在我希望代码停止时调用 io_service::cancel 来解决这个问题。
关于c++ - deadline_timer 奇怪的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19573803/
我正在研究基于 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
我是一名优秀的程序员,十分优秀!