2011-09-14 32 views
0

我的問題是關於正在運行的最後期限計數器,等待由相同函數表示完成一些操作,但我知道dont't,在釋放我的線和期限由對象截止時間安全完成奧德中斷後出。什麼時候可以發生?超時後何處銷燬線程和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.

回答

2

因爲他們的立場計時器是毫無意義的 - 只有你知道它們的意思和他們所應該做的 - 所以你必須決定要取消什麼。至於清理,追究他們在scoped_ptrshared_ptr,他們將被自動地清理時範圍/最後一個引用完成。

+0

+1'shared_ptr'並自動 –