超时后在哪里销毁thread和deadline_timer对象?
我的问题是关于正在运行的截止时间计时器,它等待由相同函数表示的某些操作完成:但我不知道在安全完成或中断后在哪里释放我的线程和截止时间对象到截止时间为止。那什么时候可以发生呢?
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.
my question is about a running deadline timers which wait for some operations represented by the same function to finish: But i dont't know, where to free my thread and deadline object after a safe finish oder an interrupt by deadline time out. When can that happen?
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
}
}
Has anyone some advice? Graceful, B.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
目前的计时器毫无意义 - 只有您知道它们的含义以及它们应该做什么 - 因此您必须决定取消什么。至于清理,将它们保存在
scoped_ptr
或shared_ptr
中,当作用域/最后一个引用完成时,它们将被自动清理。The timers as they stand are meaningless - only you know what they mean and what they are supposed to do - so you have to decide what to cancel. As to cleaning up, hold them in a
scoped_ptr
orshared_ptr
and they will be automagically cleaned up when the scope/last reference is done.