<menu id="w8yyk"><menu id="w8yyk"></menu></menu>
  • <dd id="w8yyk"><nav id="w8yyk"></nav></dd>
    <menu id="w8yyk"></menu>
    <menu id="w8yyk"><code id="w8yyk"></code></menu>
    <menu id="w8yyk"></menu>
    <xmp id="w8yyk">
    <xmp id="w8yyk"><nav id="w8yyk"></nav>
  • 網站首頁 > 物聯資訊 > 技術分享

    boost庫學習隨記六:使用同步定時器、異步定時器、bind、成員函數回調處理、多線程的同步處理示例等

    2016-09-28 00:00:00 廣州睿豐德信息科技有限公司 閱讀
    睿豐德科技 專注RFID識別技術和條碼識別技術與管理軟件的集成項目。質量追溯系統、MES系統、金蝶與條碼系統對接、用友與條碼系統對接

    一、使用同步定時器

    這個示例程序通過展示如何在一個定時器執行一個阻塞等待。

     

     

     

    [cpp] view plaincopy在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 plaincopy在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 plaincopy在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 plaincopy在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 plaincopy在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 plaincopy在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 plaincopy在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 plaincopy在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 plaincopy在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 plaincopy在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 plaincopy在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. }
        from:
    http://blog.csdn.net/leitianjun/article/details/25740633RFID管理系統集成商 RFID中間件 條碼系統中間層 物聯網軟件集成
    最近免费观看高清韩国日本大全