超时后在哪里销毁thread和deadline_timer对象?

发布于 2024-12-04 06:27:27 字数 1230 浏览 0 评论 0原文

我的问题是关于正在运行的截止时间计时器,它等待由相同函数表示的某些操作完成:但我不知道在安全完成或中断后在哪里释放我的线程和截止时间对象到截止时间为止。那什么时候可以发生呢?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时光清浅 2024-12-11 06:27:27

目前的计时器毫无意义 - 只有您知道它们的含义以及它们应该做什么 - 因此您必须决定取消什么。至于清理,将它们保存在 scoped_ptrshared_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 or shared_ptr and they will be automagically cleaned up when the scope/last reference is done.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文