我的问题是关于一个正在运行的截止时间计时器,它等待由 same 函数表示的一些操作完成:但我不知道,安全完成后在哪里释放我的线程和截止时间对象在截止时间超时前发出中断命令。什么时候会发生?
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
中,当范围/最后引用完成时,它们将自动清理。
关于c++ - 超时后在哪里销毁thread和deadline_timer对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7413362/