2011-09-14 8 views
0

私の質問は、実行中の期限についてです。で表されるいくつかの操作を待つタイマ同じの機能が終了します。しかし、私は安全な仕上げの後にスレッドとデッドラインオブジェクトを解放する場所を知っていません。でる。それはいつできるのですか?タイムアウト後に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.

答えて

2

タイマーは意味がありません - 彼らは何を意味するのか、何をするべきかを知っているだけなので、キャンセルするものを決める必要があります。クリーンアップに関しては、scoped_ptrまたはshared_ptrでそれらを保持し、スコープ/最後の参照が行われると自動的にクリーンアップされます。

+0

+1は 'shared_ptr'と自動的に –

関連する問題