【Boost】boost庫asio詳解4——deadline_timer使用說明

來源:互聯網
上載者:User

deadline_timer和socket一樣,都用io_service作為建構函式的參數。也即,在其上進行非同步作業,都將導致和io_service所包含的iocp相關聯。這同樣意味著在析構 io_service之前,必須析構關聯在這個io_service上的deadline_timer。
1. 建構函式

在構造deadline_timer時指定時間。

basic_deadline_timer(    boost::asio::io_service & io_service);basic_deadline_timer(    boost::asio::io_service & io_service,    const time_type & expiry_time);basic_deadline_timer(    boost::asio::io_service & io_service,    const duration_type & expiry_time);

注意後兩種的區別。以下2種用法是等價的:

boost::asio::deadline_timer t(io, boost::posix_time::microsec_clock::universal_time()+boost::posix_time::seconds(5));boost::asio::deadline_timer t(io, boost::posix_time::seconds(5));

前者是絕對時間,後者是相對時間。
2. 同步

一個deadline_timer只維護一個逾時時間,一個deadline_timer不同時維持多個定時器。

void wait();void wait(boost::system::error_code& ec);

這是個同步等待函數,例如:

boost::asio::io_service io;boost::asio::deadline_timer t(io, boost::posix_time::seconds(5));t.wait();

由於不涉及到非同步,該函數和io_service沒什麼關係。這個函數在windows下的實現就只是簡單的sleep。因此也就不存在cancel之說。
3. 非同步

template<typename WaitHandler>void async_wait(WaitHandler handler);

注意這個error很重要,表明這個handler是因為逾時被執行還是因為被cancel。
符合2種情況之一,handler被執行:逾時或者被cancel。
這同時隱含的說明了除非io.stop被調用,否則handler一定會被執行。即便是被cancel。
被cancel有多種方法,直接調用cancel或者調用expires_at,expires_from_now重新設定逾時時間。
4. 例子

namespace{void print(const boost::system::error_code&){PRINT_DEBUG("Hello, world!");}void handle_wait(const boost::system::error_code& error,                     boost::asio::deadline_timer& t,                      int& count){if(!error){PRINT_DEBUG(count);if(count++ < 5){t.expires_from_now(boost::posix_time::seconds(3));t.async_wait(boost::bind(handle_wait,                      boost::asio::placeholders::error,                                         boost::ref(t), boost::ref(count)));if (count == 3){t.cancel();}}}} }// 同步方法void test_timer_syn(){boost::asio::io_service ios;boost::asio::deadline_timer t(ios, boost::posix_time::seconds(3));PRINT_DEBUG(t.expires_at());t.wait();PRINT_DEBUG("Hello syn deadline_timer!");}// 非同步方法呼叫: 3秒後執行print方法. void test_timer_asyn(){boost::asio::io_service io;boost::asio::deadline_timer t(io, boost::posix_time::seconds(3));t.async_wait(print);PRINT_DEBUG("After async_wait...");io.run();}// 非同步迴圈執行方法: void test_timer_asyn_loop(){boost::asio::io_service io;boost::asio::deadline_timer t(io);size_t a = t.expires_from_now(boost::posix_time::seconds(1));int count = 0;t.async_wait(boost::bind(handle_wait,                              boost::asio::placeholders::error,                             boost::ref(t),                             boost::ref(count)));io.run();    }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.