在超时后销毁thread和deadline_timer对象
Where to destroy thread and deadline_timer objects after timeout?
我的问题是关于一个正在运行的截止日期计时器,等待由相同的函数表示的一些操作完成:但我不知道,在安全完成订单后释放我的线程和截止日期对象,截止日期超时中断。什么时候能实现呢?
boost::asio::deadline_timer* mDeadline1;
boost::asio::deadline_timer* mDeadline2;
boost::thread* mThread1;
boost::thread* mThread2;
// start deadline timers
mDeadline1 = new boost::asio::deadline_timer(_io_service, boost::posix_time::seconds(2));
mDeadline1->async_wait(&MyClass::DeadlineTimedOut, this);
mDeadline2 = new boost::asio::deadline_timer(_io_service, boost::posix_time::seconds(2));
mDeadline2->async_wait(&MyClass::DeadlineTimedOut, this);
// Run operations in threads
mThread1 = new boost::thread(&MyClass::DoSomething, this);
mThread2 = new boost::thread(&MyClass::DoSomething, this);
// ...
void MyClass::DoSomething() {
// Do something time expensive and sleep, etc. for interrupt point ...
// which to cancel here!?
mDeadline->cancel();
delete mDeadline;
}
void MyClass::DeadlineTimedOut(const boost::system::error_code& pErrorCode) {
if(pErrorCode == 0) { // deadline timed out
// which to interrupt here?
mThread->interrupt();
}
if(pErrorCode == 995) { // deadline cancelled from outside
}
}
有人有什么建议吗?优雅,b .
计时器是没有意义的——只有你知道它们的意思和它们应该做什么——所以你必须决定取消什么。至于清理,将它们保存在scoped_ptr
或shared_ptr
中,当范围/最后一次引用完成时,它们将自动清理。
相关文章:
- 是否需要在 Timer.5 boost::asio 教程中运行新线程?
- libevent是否允许在不同的线程中运行timer/io的回调
- QGraphicsView / Qgraphicsscene Timer事件不起作用
- 使用boost::asio::deadline_Timer实现通用Timer类时出现问题
- 创建 Timer 类时,我应该使用什么数据类型来保存值?(C/C++)
- C++ `Timer` class implementation
- 用c++编写Timer类
- Qt QLCDNumber Timer
- 在 c# 中是否有等效的 boost::timer::cpu_timer
- NachOS timer.cc 构造函数
- 在 c++ 中是否有像 timer 和 timertask 这样的东西,就像 java 一样
- c++计时器,相当于.NET System.Threading.timer
- 开始使用boost-cpu_timer时,错误:“boost::timer::cpu_timer”尚未声明
- 如何从boost::timer::cpu_timer获取以秒为单位的运行时间
- 如何为一些类(如timer类)编写单元测试
- 如何在c++中创建Timer任务
- Boost Timer 24小时格式
- 如何重启boost::timer::cpu_timer
- 在Timer ISR内使用FreeRTOS中的全局值
- Linux c++ timer周期性回调