【问题标题】:Where to destroy thread and deadline_timer objects after timeout?超时后在哪里销毁线程和deadline_timer对象?
【发布时间】:2011-09-14 08:27:18
【问题描述】:

我的问题是关于等待由 same 函数表示的某些操作完成的运行期限计时器:但我不知道,在安全完成后在哪里释放我的线程和期限对象oder 在截止日期超时之前中断。什么时候会发生这种情况?

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.

【问题讨论】:

    标签: c++ multithreading boost boost-asio


    【解决方案1】:

    当前的计时器毫无意义——只有你知道它们的含义和它们应该做什么——所以你必须决定取消什么。至于清理,请将它们放在scoped_ptrshared_ptr 中,当范围/最后一次引用完成时,它们将被自动清理。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-11
      • 2010-10-15
      • 2015-08-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-31
      • 1970-01-01
      相关资源
      最近更新 更多