简单的几个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

  

 

 

  

 

posted @ 2015-02-11 00:05  两仪清风  阅读(8559)  评论(0编辑  收藏  举报