boost库学习随记六:使用同步定时器、异步定时器、bind、成员函数回调处理、多线程的同步处理示例等

简介: 一、使用同步定时器 这个示例程序通过展示如何在一个定时器执行一个阻塞等待。       [cpp] view plaincopy   //makefile   #---------------------------------------------------...

一、使用同步定时器

这个示例程序通过展示如何在一个定时器执行一个阻塞等待。

 

 

 

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  1. //makefile  
  2. #----------------------------------------------------------  
  3. #makefile helloworld测试用例  
  4. #  
  5. #  
  6. #  
  7. #  
  8. #-----------------------------------------------------------  
  9. ggg=g++  
  10. exe=asiotimer  
  11.   
  12. #所有的.o文件写在这里  
  13. obj = asiotimer.o  
  14.   
  15. #所要关联的cpp文件写在这里  
  16. cpp = asiotimer.cpp  
  17.   
  18.   
  19. #加入库文件  
  20. libso = -lboost_thread -lboost_system  
  21.   
  22. $(exe):$(obj)  
  23.         @echo "链接开始................"  
  24.         $(ggg) $(libso) -o $(exe) $(obj)  
  25.   
  26.   
  27. hw.o : $(cpp)  
  28.         @echo "编译开始................"  
  29.         $(ggg) -std=c++11 -c $(cpp)  
  30.   
  31.   
  32.   
  33. .PHONY : clean cleanall  
  34. cleanall:  
  35.         @echo "开始make all..........."  
  36.         -rm -rf $(exe) $(obj)  
  37.   
  38. clean:  
  39.         @echo "开始清理................"  
  40.         -rm -rf $(obj)  

2、asiotimer.h头文件

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  1. //asiotimer.h  
  2. #ifndef __ASIOTIMER__H__  
  3. #define __ASIOTIMER__H__  
  4. #include <iostream>  
  5. #include <boost/asio.hpp>  
  6. //#define BOOST_DATE_TIME_SOURCE  
  7. #include "boost/date_time/posix_time/posix_time.hpp"  
  8.   
  9.   
  10. #endif  

3、asiotimer.cpp文件

 

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  1. //asiotimer.cpp  
  2. #include "asiotimer.h"  
  3. int main()  
  4. {  
  5.         boost::asio::io_service io;  
  6.         boost::asio::deadline_timer t(io,boost::posix_time::seconds(5));  
  7.         t.wait();  
  8.   
  9.         std::cout<<"hello,world\n";  
  10.         return 0;  
  11. }  

 

 

 

 

二、使用异步定时器示例

本示例程序演示了如何使用Asio的异步回调功能由示例一修改程序 ,开启计时器执行一个异步等待。

1、makefile文件

makefile 与示例一基本相同,只需要修改
exe=asiotest2

#所有的.o文件写在这里
obj = asiotest2.o

#所要关联的cpp文件写在这里
cpp = asiotest2.cpp

 

2、asiotest2.h

 

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  1. #ifndef __ASIOTEST2__H__  
  2. #define __ASIOTEST2__H__  
  3. #include <iostream>  
  4. #include <boost/asio.hpp>  
  5. #include <boost/date_time/posix_time/posix_time.hpp>  
  6. void print(const boost::system::error_code& );  
  7.   
  8.   
  9. #endif  

 

 

 

 

 

3、asiotest2.cpp

 

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  1. #include "asiotest2.h"  
  2. using namespace std;  
  3. using namespace boost;  
  4.   
  5. void print(const boost::system::error_code& )  
  6. {  
  7.         std::cout<<"hello,world!\n";  
  8. }  
  9. int main()  
  10. {  
  11.         boost::asio::io_service io;  
  12.         boost::asio::deadline_timer t(io,boost::posix_time::seconds(5));  
  13.         t.async_wait(&print);  
  14.         io.run();  
  15.         return 0;  
  16. }  

 

 

 

 

 

三、绑定参数到处理程序

在本示例中,我们将在示例二修改程序,使定时器每秒被激活一次。这将显示如何传递额外的参数给你的处理函数。

1、makefile 文件同示例二makefile修改方法

 

2、头文件

 

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  1. #ifndef __ASIOTEST3__H__  
  2. #define __ASIOTEST3__H__  
  3. #include <iostream>  
  4. #include <boost/asio.hpp>  
  5. #include <boost/bind.hpp>  
  6. #include <boost/date_time/posix_time/posix_time.hpp>  
  7.   
  8. #endif  

 

 

 

 

 

3、CPP文件

 

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  1. #include "asiotest3.h"  
  2.   
  3. void print(const boost::system::error_code&,  
  4.         boost::asio::deadline_timer* t,int* count)  
  5. {  
  6.         if(*count<5)  
  7.         {  
  8.                 std::cout<<*count<<"\n";  
  9.                 ++(*count);  
  10.                 t->expires_at(t->expires_at() + boost::posix_time::seconds(1));  
  11.                 t->async_wait(boost::bind(print,  
  12.                         boost::asio::placeholders::error,t,count));  
  13.         }  
  14. }  
  15.   
  16. int main()  
  17. {  
  18.         boost::asio::io_service io;  
  19.         int count=0;  
  20.         boost::asio::deadline_timer t(io,boost::posix_time::seconds(1));  
  21.         t.async_wait(boost::bind(print,boost::asio::placeholders::error,  
  22.                         &t,&count));  
  23.         io.run();  
  24.         std::cout<<"Final count is" <<count<<"\n";  
  25.         return 0;  
  26. }  

 

 

 

 

 

四、使用成员函数做为处理程序示例

在本示例中,我们将看到如何使用一个类的成员函数作为回调处理程序。

 

1、makefile 同上面示例

2、头文件

 

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  1. #ifndef __ASIOTEST4__H__  
  2. #define __ASIOTEST4__H__  
  3. #include <iostream>  
  4. #include <boost/asio.hpp>  
  5. #include <boost/bind.hpp>  
  6. #include <boost/date_time/posix_time/posix_time.hpp>  
  7.   
  8. class printer  
  9. {  
  10. public:  
  11.         printer(boost::asio::io_service& io)  
  12.                 :timer_(io,boost::posix_time::seconds(1)),  
  13.                 count_(0)  
  14.         {  
  15.                 timer_.async_wait(boost::bind(&printer::print,this));  
  16.         }  
  17.         ~printer()  
  18.         {  
  19.                 std::cout<<"Final count is "<<count_<<"\n";  
  20.         }  
  21.   
  22.         void print()  
  23.         {  
  24.                 if(count_<5)  
  25.                 {  
  26.                         std::cout<<count_<<std::endl;  
  27.                         ++count_;  
  28.   
  29.                         timer_.expires_at(timer_.expires_at()+boost::posix_time::seconds(1));  
  30.                         timer_.async_wait(boost::bind(&printer::print,this));  
  31.                 }  
  32.         }  
  33. private:  
  34.         boost::asio::deadline_timer timer_;  
  35.         int count_;  
  36.   
  37. };  

 

 

3、cpp文件

 

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  1. #include "asiotest4.h"  
  2. int main()  
  3. {  
  4.         boost::asio::io_service io;  
  5.         printer p(io);  
  6.         io.run();  
  7.         return 0;  
  8. }  

 

 

 

五、多线程的同步处理示例

本示例演示boost::asio::strand 在多线程程序中同步回调处理程

1、makefile同上

2、头文件

 

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  1. #ifndef __ASIOTEST5__H__  
  2. #define __ASIOTEST5__H__  
  3. #include <iostream>  
  4. #include <boost/asio.hpp>  
  5. #include <boost/thread/thread.hpp>  
  6. #include <boost/bind.hpp>  
  7. #include <boost/date_time/posix_time/posix_time.hpp>  
  8.   
  9. class printer  
  10. {  
  11. public:  
  12.         printer(boost::asio::io_service& io):strand_(io),  
  13.                 timer1_(io,boost::posix_time::seconds(1)),  
  14.                 timer2_(io,boost::posix_time::seconds(1)),count_(0)  
  15.         {  
  16.                 timer1_.async_wait(strand_.wrap(boost::bind(&printer::print1,this)));  
  17.                 timer2_.async_wait(strand_.wrap(boost::bind(&printer::print2,this)));  
  18.         }  
  19.         ~printer()  
  20.         {  
  21.                 std::cout<<"Final count is " <<count_<<std::endl;  
  22.         }  
  23.   
  24.         void print1()  
  25.         {  
  26.                 if(count_ < 10)  
  27.                 {  
  28.                         std::cout<<"Timer 1: "<<count_<<std::endl;  
  29.                         ++count_;  
  30.   
  31.                         timer1_.expires_at(timer1_.expires_at() + boost::posix_time::seconds(1));  
  32.                         timer1_.async_wait(strand_.wrap(boost::bind(&printer::print1,this)));  
  33.   
  34.                 }  
  35.         }  
  36.   
  37.         void print2()  
  38.         {  
  39.   
  40.                 if(count_ < 10)  
  41.                 {  
  42.                         std::cout<<"Timer 2: " <<count_<<std::endl;  
  43.                         ++count_;  
  44.                         timer2_.expires_at(timer2_.expires_at() + boost::posix_time::seconds(1));  
  45.                         timer2_.async_wait(strand_.wrap(boost::bind(&printer::print2,this)));  
  46.   
  47.                 }  
  48.         }  
  49. private:  
  50.         boost::asio::strand strand_;  
  51.         boost::asio::deadline_timer timer1_;  
  52.         boost::asio::deadline_timer timer2_;  
  53.         int count_;  
  54. };  
  55.   
  56. #endif   

 

 

 

3、CPP文件

 

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  1. #include "asiotest5.h"  
  2. int main()  
  3. {  
  4.   boost::asio::io_service io;  
  5.   printer p(io);  
  6.   boost::thread t(boost::bind(&boost::asio::io_service::run,&io));  
  7.   io.run();  
  8.   t.join();  
  9.   return 0;  
  10. }
    1. from:
http://blog.csdn.net/leitianjun/article/details/25740633
目录
相关文章
|
22天前
|
Linux C++
LInux下Posix的传统线程示例
LInux下Posix的传统线程示例
19 1
|
1月前
|
Java 云计算
Java多线程编程中的同步与互斥机制探析
在当今软件开发领域,多线程编程是一项至关重要的技能。本文将深入探讨Java中的同步与互斥机制,分析其在多线程环境下的应用及实现原理,帮助读者更好地理解并运用这一关键技术。
24 4
|
1月前
|
Java 调度 C#
C#学习系列相关之多线程(一)----常用多线程方法总结
C#学习系列相关之多线程(一)----常用多线程方法总结
|
1月前
|
安全 编译器 C#
C#学习相关系列之多线程---lock线程锁的用法
C#学习相关系列之多线程---lock线程锁的用法
|
1月前
|
C#
C#学习相关系列之多线程---ConfigureAwait的用法
C#学习相关系列之多线程---ConfigureAwait的用法
|
1月前
|
C#
C#学习相关系列之多线程---TaskCompletionSource用法(八)
C#学习相关系列之多线程---TaskCompletionSource用法(八)
|
1月前
|
Java C#
C#学习系列相关之多线程(五)----线程池ThreadPool用法
C#学习系列相关之多线程(五)----线程池ThreadPool用法
|
1月前
|
并行计算 安全 程序员
【C++】—— C++11之线程库
【C++】—— C++11之线程库
|
28天前
|
Python
Python学习之路 02 之分支结构
Python学习之路 02 之分支结构
47 0
Python学习之路 02 之分支结构
|
28天前
|
Java Python 开发者
Python 学习之路 01基础入门---【Python安装,Python程序基本组成】
线程池详解与异步任务编排使用案例-xian-cheng-chi-xiang-jie-yu-yi-bu-ren-wu-bian-pai-shi-yong-an-li
76 2
Python 学习之路 01基础入门---【Python安装,Python程序基本组成】

热门文章

最新文章