开发者

Where to destroy thread and deadline_timer objects after timeout?

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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜