Boost::asio



deadline_timer::cancel

deadline_timerをcancelしても、ハンドラは呼ばれる。しかも、cancel直後に呼ばれる。その時、引数のerror_codeに、errorがセットされているので、ちゃんとチェックすること。
エラー内容は、「"ハンドラ名"スレッドの終了またはアプリケーションの要求によって、I/O 処理は中止されました。」

void HandleCanceler(const boost::system::error_code& error,
                    boost::asio::deadline_timer* timer) {
    std::cout << "HandleCanceler";

    if (error)
        std::cout << " - error";
    else
        timer->cancel();

    std::cout << std::endl;
}
void HandleTimer(const boost::system::error_code& error) {
    std::cout << "HandleTimer";

    if (error)
        std::cout << " - error";

    std::cout << std::endl;
}
int _tmain(int argc, _TCHAR* argv[]) {
 
    boost::asio::io_service iosv;
    boost::asio::deadline_timer timer(
        iosv, boost::posix_time::seconds(3));
    boost::asio::deadline_timer canceler(
        iosv, boost::posix_time::seconds(1));

    std::cout << "Start" << std::endl;
    timer.async_wait(&HandleTimer);
    canceler.async_wait(
        boost::bind(&HandleCanceler, _1, &timer));
    std::cout << "Block" << std::endl;
    iosv.run();
	
    std::cout << "Done" << std::endl;
    getchar();
	
    return 0;
}
最終更新:2009年01月08日 09:30