簡單的幾個Boost定時器


  boost 的asio庫里有幾個定時的器,老的有 deadline_timer , 還有三個可配合 C++11 的 chrono 使用的 high_resolution_timer  、 steady_timer  和 system_timer 。

  老的 deadline_timer 我不太想用了,因為用起來沒有后面三個好用。但是后面三個沒有 C++ 11 也不好用。

  C++ 之父 曾建議 老的程序應該用最新的編譯器在C++ 11的標准下重新編譯一遍,當然,他還補充說是在成本上允許的情況下,如果你的老程序是長年累月一點點編譯 和增長起來的,重新編譯可能不現實。 但是新的 C++ 項目,應該優先使用 C++11的標准來實現。

  這里僅簡單介紹一下 后三個 中的任意一個,因為它們實現相似到很難看出區別。除非在極端的條件下,后三個用哪一個都差不多,如果你不知道用哪個,那就用  steady_timer 吧。

 

  下面看示例:

 

  

 1 #include <iostream>
 2 #include <chrono>
 3 #include <thread>
 4 #include <boost/asio.hpp>
 5 
 6 
 7 #include <boost/asio/high_resolution_timer.hpp >
 8 #include <boost/asio/steady_timer.hpp >
 9 #include <boost/asio/system_timer.hpp >
10 
11 
12 class printer {
13     private:
14         boost::asio::io_service io_;
15         boost::asio::steady_timer timer_;
16         int count_;
17         void print() {
18             if (count_ < 500) {
19                 std::cout << count_ << "\n";
20                 ++count_;
21 
22                 timer_.expires_from_now(std::chrono::milliseconds (50));
23                 timer_.async_wait(std::bind(&printer::print, this));
24             }
25             else
26             {
27                 std::cout << "Final count is " << count_ << "\n";
28                 delete this;
29             }
30         }
31         void run() {
32             timer_.expires_from_now(std::chrono::milliseconds (50));
33             timer_.async_wait(std::bind(&printer::print, this));
34             io_.run();
35         }
36         printer()
37             : timer_(io_),
38               count_(0) {
39 
40         }
41         ~printer() {
42             
43         }
44 
45     public:
46 
47         static printer* Create(){
48             return new printer;
49         }
50     
51         void start() {
52             std::thread t;
53             t = std::thread(std::mem_fn(&printer::run), this);
54             t.detach();
55         }
56 };
57 void foo()
58 {
59     printer *p = printer::Create();
60     p->start();
61 }
62 int main() {
63     foo();
64     std::cin.get();
65     return 0;
66 }
View Code

  

  該代碼在 windows 下使用 tdm-gcc 4.8.1 配合 boost 1.57.0 編譯通過並運行。 鏈接時需要 wsock32 庫 和 boost-system 庫 ( -lboost_system -lwsock32)

  

  源代碼存為main.cpp,下面是linux下的makefile (centos 7.0 系統自帶的 gcc 和 boost 1.5.70

 

CXX             = g++
BIN             = timer
OBJ             = main.o
LINKOBJ         = -pthread -lboost_system
CXXINC          = 
CXXFLAGS        = $(CXXINC) -std=c++11 -O3 -Wall
RM              = rm -f

$(BIN):$(OBJ)
        $(CXX)  $(CXXFLAGS) $(OBJ) $(LINKOBJ) -o $(BIN)

%.o:%.cpp
        $(CXX) $(CXXFLAGS) -c $<

clean:
        $(RM) $(OBJ) $(BIN)

rebuild:
        make clean
        make
View Code

 

  

  定時器的用法也是比較簡單的,基本上分三步。創建 io_service , 創建timer 並設置等待時間, 調用wait 或async_wait 等待.

  

  

  其中wait是同步等待,async_wait是異步等待,需要給一個回調給它。

 

  同一個 io_service 可以同時給多個 timer使下,看下面的示例

  

  

 1 #include <iostream>
 2 #include <chrono>
 3 #include <thread>
 4 #include <boost/asio.hpp>
 5 
 6 
 7 #include <boost/asio/high_resolution_timer.hpp >
 8 #include <boost/asio/steady_timer.hpp >
 9 #include <boost/asio/system_timer.hpp >
10 
11 
12 class printer2 {
13     private:
14         boost::asio::steady_timer timer_;
15         int count_;
16         void print() {
17             if (count_ < 10) {
18                 std::cout << count_ << "\n";
19                 ++count_;
20 
21                 timer_.expires_from_now(std::chrono::milliseconds (500));
22                 timer_.async_wait(std::bind(&printer2::print, this));
23             }
24             else
25             {
26                 std::cout << "Final count is " << count_ << "\n";
27                 delete this;
28             }
29         }
30         printer2(boost::asio::io_service &io)
31             : timer_(io,std::chrono::milliseconds (500)),
32               count_(0) {
33                 timer_.async_wait(std::bind(&printer2::print, this));
34 
35         }
36         ~printer2() {
37             
38         }
39 
40     public:
41 
42         static printer2* Create(boost::asio::io_service &io){
43             return new printer2(io);
44         }
45     
46 
47 };
48 
49 int main() {
50     boost::asio::io_service io;
51     printer2::Create(io);
52     printer2::Create(io);
53     printer2::Create(io);
54     printer2::Create(io);
55     io.run();
56     std::cin.get();
57     return 0;
58 }
View Code

 

  下面附上我打包好的devcpp 綠色包

  

 

  DevC++ 5.9.1 with Boost 1.57.0

  

 

 

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM