Boost-asio之四

来源:互联网 发布:csgo职业选手优化fps 编辑:程序博客网 时间:2024/06/06 02:02
     下面介绍Boost.Asio的异常处理和计时器(timer)

一  Exceptions

        Boost.Asio提供两种异常处理方式:通过try/catch的方式获取异常或者通过错误码的方式。

        下面是一个通过try/catch的方式的一个例子:

[cpp] view plain copy print?在CODE上查看代码片派生到我的代码片
  1. #include <boost/asio.hpp>   
  2. #include <boost/shared_ptr.hpp>   
  3. #include <boost/thread.hpp>  
  4. #include <boost/thread/mutex.hpp>   
  5. #include <boost/bind.hpp>  
  6. #include <iostream>   
  7.   
  8. boost::mutex global_stream_lock;  
  9.   
  10. void WorkerThread( boost::shared_ptr< boost::asio::io_service > io_service )  
  11. {  
  12.     global_stream_lock.lock();  
  13.     std::cout << "[" << boost::this_thread::get_id()  
  14.         << "] Thread Start" << std::endl;  
  15.     global_stream_lock.unlock();  
  16.   
  17.     try  
  18.     {  
  19.         io_service->run();  
  20.     }  
  21.     catch( std::exception & ex )  
  22.     {  
  23.         global_stream_lock.lock();  
  24.         std::cout << "[" << boost::this_thread::get_id()  
  25.             << "] Exception: " << ex.what() << std::endl;  
  26.         global_stream_lock.unlock();  
  27.     }  
  28.   
  29.     global_stream_lock.lock();  
  30.     std::cout << "[" << boost::this_thread::get_id()  
  31.         << "] Thread Finish" << std::endl;  
  32.     global_stream_lock.unlock();  
  33. }  
  34.   
  35. void RaiseAnException( boost::shared_ptr< boost::asio::io_service > io_service )  
  36. {  
  37.     global_stream_lock.lock();  
  38.     std::cout << "[" << boost::this_thread::get_id()  
  39.         << "] " << __FUNCTION__ << std::endl;  
  40.     global_stream_lock.unlock();  
  41.   
  42.     io_service->post( boost::bind( &RaiseAnException, io_service ) );  
  43.   
  44.     throw( std::runtime_error( "Oops!" ) );  
  45. }  
  46.   
  47. int main( int argc, char * argv[] )  
  48. {  
  49.     boost::shared_ptr< boost::asio::io_service > io_service(  
  50.         new boost::asio::io_service  
  51.         );  
  52.     boost::shared_ptr< boost::asio::io_service::work > work(  
  53.         new boost::asio::io_service::work( *io_service )  
  54.         );  
  55.   
  56.     global_stream_lock.lock();  
  57.     std::cout << "[" << boost::this_thread::get_id()   
  58.         << "] The program will exit when all work has finished." << std::endl;  
  59.     global_stream_lock.unlock();  
  60.   
  61.     boost::thread_group worker_threads;  
  62.     forint x = 0; x < 2; ++x )  
  63.     {  
  64.         worker_threads.create_thread( boost::bind( &WorkerThread, io_service ) );  
  65.     }  
  66.   
  67.     io_service->post( boost::bind( &RaiseAnException, io_service ) );  
  68.   
  69.     worker_threads.join_all();  
  70.   
  71.     return 0;  
  72. }  

        看上去程序好像要不停的输出oops的异常信息,其实不是,只输出了两次。这是因为抛出的异常被当前线程catch,处理,从而中断了线程继续运行。将上面的程序进行如下的修改可以更直接的说明这点:

       

[cpp] view plain copy print?在CODE上查看代码片派生到我的代码片
  1. #include <boost/asio.hpp>   
  2. #include <boost/shared_ptr.hpp>   
  3. #include <boost/thread.hpp>  
  4. #include <boost/thread/mutex.hpp>   
  5. #include <boost/bind.hpp>  
  6. #include <iostream>   
  7.   
  8. boost::mutex global_stream_lock;  
  9.   
  10. void WorkerThread( boost::shared_ptr< boost::asio::io_service > io_service )  
  11. {  
  12.     global_stream_lock.lock();  
  13.     std::cout << "[" << boost::this_thread::get_id()  
  14.         << "] Thread Start" << std::endl;  
  15.     global_stream_lock.unlock();  
  16.   
  17.     try  
  18.     {  
  19.         io_service->run();  
  20.     }  
  21.     catch( std::exception & ex )  
  22.     {  
  23.         global_stream_lock.lock();  
  24.         std::cout << "[" << boost::this_thread::get_id()  
  25.             << "] Exception: " << ex.what() << std::endl;  
  26.         global_stream_lock.unlock();  
  27.     }  
  28.   
  29.     global_stream_lock.lock();  
  30.     std::cout << "[" << boost::this_thread::get_id()  
  31.         << "] Thread Finish" << std::endl;  
  32.     global_stream_lock.unlock();  
  33. }  
  34.   
  35. void RaiseAnException( boost::shared_ptr< boost::asio::io_service > io_service )  
  36. {  
  37.     global_stream_lock.lock();  
  38.     std::cout << "[" << boost::this_thread::get_id()  
  39.         << "] " << __FUNCTION__ << std::endl;  
  40.     global_stream_lock.unlock();  
  41.   
  42.     io_service->post( boost::bind( &RaiseAnException, io_service ) );  
  43.     io_service->post( boost::bind( &RaiseAnException, io_service ) );//多post一次  
  44.   
  45.     throw( std::runtime_error( "Oops!" ) );  
  46. }  
  47.   
  48. int main( int argc, char * argv[] )  
  49. {  
  50.     boost::shared_ptr< boost::asio::io_service > io_service(  
  51.         new boost::asio::io_service  
  52.         );  
  53.     boost::shared_ptr< boost::asio::io_service::work > work(  
  54.         new boost::asio::io_service::work( *io_service )  
  55.         );  
  56.   
  57.     global_stream_lock.lock();  
  58.     std::cout << "[" << boost::this_thread::get_id()   
  59.         << "] The program will exit when all work has finished." << std::endl;  
  60.     global_stream_lock.unlock();  
  61.   
  62.     boost::thread_group worker_threads;  
  63.     forint x = 0; x < 3; ++x )//改为3  
  64.     {  
  65.         worker_threads.create_thread( boost::bind( &WorkerThread, io_service ) );  
  66.     }  
  67.   
  68.     io_service->post( boost::bind( &RaiseAnException, io_service ) );  
  69.   
  70.     worker_threads.join_all();  
  71.   
  72.     return 0;  
  73. }  

多post一次而不将线程数改为3,多post的任务将得不到处理,这是因为exception的产生中断了线程的执行。

        下面再来看一个关于错误码(error variable)的例子:

       

[cpp] view plain copy print?在CODE上查看代码片派生到我的代码片
  1. #include <boost/asio.hpp>   
  2. #include <boost/shared_ptr.hpp>   
  3. #include <boost/thread.hpp>  
  4. #include <boost/thread/mutex.hpp>   
  5. #include <boost/bind.hpp>  
  6. #include <iostream>   
  7.   
  8. boost::mutex global_stream_lock;  
  9.   
  10. void WorkerThread( boost::shared_ptr< boost::asio::io_service > io_service )  
  11. {  
  12.     global_stream_lock.lock();  
  13.     std::cout << "[" << boost::this_thread::get_id()  
  14.         << "] Thread Start" << std::endl;  
  15.     global_stream_lock.unlock();  
  16.   
  17.     boost::system::error_code ec;  
  18.     io_service->run( ec );  
  19.   
  20.     if( ec )  
  21.     {  
  22.         global_stream_lock.lock();  
  23.         std::cout << "[" << boost::this_thread::get_id()  
  24.             << "] Exception: " << ec << std::endl;  
  25.         global_stream_lock.unlock();  
  26.     }  
  27.   
  28.     global_stream_lock.lock();  
  29.     std::cout << "[" << boost::this_thread::get_id()  
  30.         << "] Thread Finish" << std::endl;  
  31.     global_stream_lock.unlock();  
  32. }  
  33.   
  34. void RaiseAnException( boost::shared_ptr< boost::asio::io_service > io_service )  
  35. {  
  36.     global_stream_lock.lock();  
  37.     std::cout << "[" << boost::this_thread::get_id()  
  38.         << "] " << __FUNCTION__ << std::endl;  
  39.     global_stream_lock.unlock();  
  40.   
  41.     io_service->post( boost::bind( &RaiseAnException, io_service ) );  
  42.   
  43.     throw( std::runtime_error( "Oops!" ) );  
  44. }  
  45.   
  46. int main( int argc, char * argv[] )  
  47. {  
  48.     boost::shared_ptr< boost::asio::io_service > io_service(  
  49.         new boost::asio::io_service  
  50.         );  
  51.     boost::shared_ptr< boost::asio::io_service::work > work(  
  52.         new boost::asio::io_service::work( *io_service )  
  53.         );  
  54.   
  55.     global_stream_lock.lock();  
  56.     std::cout << "[" << boost::this_thread::get_id()   
  57.         << "] The program will exit when all work has finished." << std::endl;  
  58.     global_stream_lock.unlock();  
  59.   
  60.     boost::thread_group worker_threads;  
  61.     forint x = 0; x < 2; ++x )  
  62.     {  
  63.         worker_threads.create_thread( boost::bind( &WorkerThread, io_service ) );  
  64.     }  
  65.   
  66.     io_service->post( boost::bind( &RaiseAnException, io_service ) );  
  67.   
  68.     worker_threads.join_all();  
  69.   
  70.     return 0;  
  71. }  

运行上面的程序会产生core dump,这是因为产生的exception没有被catch。根本原因是因为错误码的方式不会将用户自定义的异常转换为错误码,而只针对Boost.Asio的库才会起作用。针对Boost.Asio库产生的异常,如果没有使用错误码,将会上抛异常;如果使用了,将会将异常转换为对应的错误码。

        如果一个异常产生了,针对异常的类型:系统异常还是简单的文本错误,来进行相应的处理,如果是系统异常,我们需要在异常处理方法中调用stop来让io_service正常终止;如果是文本错误,则需要将io_service重新拉起来,防止线程退出。修改上面的例子,可以看得更清楚些,下面采用while的方式来将io_service重新拉起:

[cpp] view plain copy print?在CODE上查看代码片派生到我的代码片
  1. #include <boost/asio.hpp>   
  2. #include <boost/shared_ptr.hpp>   
  3. #include <boost/thread.hpp>  
  4. #include <boost/thread/mutex.hpp>   
  5. #include <boost/bind.hpp>  
  6. #include <iostream>   
  7.   
  8. boost::mutex global_stream_lock;  
  9.   
  10. void WorkerThread( boost::shared_ptr< boost::asio::io_service > io_service )  
  11. {  
  12.     global_stream_lock.lock();  
  13.     std::cout << "[" << boost::this_thread::get_id()  
  14.         << "] Thread Start" << std::endl;  
  15.     global_stream_lock.unlock();  
  16.   
  17.     whiletrue )  
  18.     {  
  19.         try  
  20.         {  
  21.             boost::system::error_code ec;  
  22.             io_service->run( ec );  
  23.             if( ec )  
  24.             {  
  25.                 global_stream_lock.lock();  
  26.                 std::cout << "[" << boost::this_thread::get_id()  
  27.                     << "] Error: " << ec << std::endl;  
  28.                 global_stream_lock.unlock();  
  29.             }  
  30.             break;  
  31.         }  
  32.         catch( std::exception & ex )  
  33.         {  
  34.             global_stream_lock.lock();  
  35.             std::cout << "[" << boost::this_thread::get_id()  
  36.                 << "] Exception: " << ex.what() << std::endl;  
  37.             global_stream_lock.unlock();  
  38.         }  
  39.     }  
  40.   
  41.     global_stream_lock.lock();  
  42.     std::cout << "[" << boost::this_thread::get_id()  
  43.         << "] Thread Finish" << std::endl;  
  44.     global_stream_lock.unlock();  
  45. }  
  46.   
  47. void RaiseAnException( boost::shared_ptr< boost::asio::io_service > io_service )  
  48. {  
  49.     global_stream_lock.lock();  
  50.     std::cout << "[" << boost::this_thread::get_id()  
  51.         << "] " << __FUNCTION__ << std::endl;  
  52.     global_stream_lock.unlock();  
  53.   
  54.     io_service->post( boost::bind( &RaiseAnException, io_service ) );  
  55.   
  56.     throw( std::runtime_error( "Oops!" ) );  
  57. }  
  58.   
  59. int main( int argc, char * argv[] )  
  60. {  
  61.     boost::shared_ptr< boost::asio::io_service > io_service(  
  62.         new boost::asio::io_service  
  63.         );  
  64.     boost::shared_ptr< boost::asio::io_service::work > work(  
  65.         new boost::asio::io_service::work( *io_service )  
  66.         );  
  67.   
  68.     global_stream_lock.lock();  
  69.     std::cout << "[" << boost::this_thread::get_id()   
  70.         << "] The program will exit when all work has finished." << std::endl;  
  71.     global_stream_lock.unlock();  
  72.   
  73.     boost::thread_group worker_threads;  
  74.     forint x = 0; x < 2; ++x )  
  75.     {  
  76.         worker_threads.create_thread( boost::bind( &WorkerThread, io_service ) );  
  77.     }  
  78.   
  79.     io_service->post( boost::bind( &RaiseAnException, io_service ) );  
  80.   
  81.     worker_threads.join_all();  
  82.   
  83.     return 0;  
  84. }  

        运行上面代码,终端上会连续输出,不停止。真正项目中这样的方式肯定不是我们希望看到的。

二 Timer

        timer在第一篇中有过介绍,先看一个的例子:

[cpp] view plain copy print?在CODE上查看代码片派生到我的代码片
  1. #include <boost/asio.hpp>   
  2. #include <boost/shared_ptr.hpp>   
  3. #include <boost/thread.hpp>  
  4. #include <boost/thread/mutex.hpp>   
  5. #include <boost/bind.hpp>  
  6. #include <iostream>   
  7.   
  8. boost::mutex global_stream_lock;  
  9.   
  10. void WorkerThread( boost::shared_ptr< boost::asio::io_service > io_service )  
  11. {  
  12.     global_stream_lock.lock();  
  13.     std::cout << "[" << boost::this_thread::get_id()  
  14.         << "] Thread Start" << std::endl;  
  15.     global_stream_lock.unlock();  
  16.   
  17.     whiletrue )  
  18.     {  
  19.         try  
  20.         {  
  21.             boost::system::error_code ec;  
  22.             io_service->run( ec );  
  23.             if( ec )  
  24.             {  
  25.                 global_stream_lock.lock();  
  26.                 std::cout << "[" << boost::this_thread::get_id()  
  27.                     << "] Error: " << ec << std::endl;  
  28.                 global_stream_lock.unlock();  
  29.             }  
  30.             break;  
  31.         }  
  32.         catch( std::exception & ex )  
  33.         {  
  34.             global_stream_lock.lock();  
  35.             std::cout << "[" << boost::this_thread::get_id()  
  36.                 << "] Exception: " << ex.what() << std::endl;  
  37.             global_stream_lock.unlock();  
  38.         }  
  39.     }  
  40.   
  41.     global_stream_lock.lock();  
  42.     std::cout << "[" << boost::this_thread::get_id()  
  43.         << "] Thread Finish" << std::endl;  
  44.     global_stream_lock.unlock();  
  45. }  
  46.   
  47. void TimerHandler( const boost::system::error_code & error )  
  48. {  
  49.     if( error )  
  50.     {  
  51.         global_stream_lock.lock();  
  52.         std::cout << "[" << boost::this_thread::get_id()  
  53.             << "] Error: " << error << std::endl;  
  54.         global_stream_lock.unlock();  
  55.     }  
  56.     else  
  57.     {  
  58.         global_stream_lock.lock();  
  59.         std::cout << "[" << boost::this_thread::get_id()  
  60.             << "] TimerHandler " << std::endl;  
  61.         global_stream_lock.unlock();  
  62.     }  
  63. }  
  64.   
  65. int main( int argc, char * argv[] )  
  66. {  
  67.     boost::shared_ptr< boost::asio::io_service > io_service(  
  68.         new boost::asio::io_service  
  69.         );  
  70.     boost::shared_ptr< boost::asio::io_service::work > work(  
  71.         new boost::asio::io_service::work( *io_service )  
  72.         );  
  73.   
  74.     global_stream_lock.lock();  
  75.     std::cout << "[" << boost::this_thread::get_id()  
  76.         << "] Press [return] to exit." << std::endl;  
  77.     global_stream_lock.unlock();  
  78.   
  79.     boost::thread_group worker_threads;  
  80.     forint x = 0; x < 2; ++x )  
  81.     {  
  82.         worker_threads.create_thread( boost::bind( &WorkerThread, io_service ) );  
  83.     }  
  84.   
  85.     boost::asio::deadline_timer timer( *io_service );  
  86.     timer.expires_from_now( boost::posix_time::seconds( 5 ) );  
  87.     timer.async_wait( TimerHandler );  
  88.   
  89.     std::cin.get();  
  90.   
  91.     io_service->stop();  
  92.   
  93.     worker_threads.join_all();  
  94.   
  95.     return 0;  
  96. }  

很简单,程序等待5秒后运行handler。如果上面的例子我们需要一个可以反复使用的timer,可以把timer做成全局的,但是全局的变量是线程不安全的。这个时候可以用智能指针来解决这一问题:

[cpp] view plain copy print?在CODE上查看代码片派生到我的代码片
  1. #include <boost/asio.hpp>   
  2. #include <boost/shared_ptr.hpp>   
  3. #include <boost/thread.hpp>  
  4. #include <boost/thread/mutex.hpp>   
  5. #include <boost/bind.hpp>  
  6. #include <iostream>   
  7.   
  8. boost::mutex global_stream_lock;  
  9.   
  10. void WorkerThread( boost::shared_ptr< boost::asio::io_service > io_service )  
  11. {  
  12.     global_stream_lock.lock();  
  13.     std::cout << "[" << boost::this_thread::get_id()  
  14.         << "] Thread Start" << std::endl;  
  15.     global_stream_lock.unlock();  
  16.   
  17.     whiletrue )  
  18.     {  
  19.         try  
  20.         {  
  21.             boost::system::error_code ec;  
  22.             io_service->run( ec );  
  23.             if( ec )  
  24.             {  
  25.                 global_stream_lock.lock();  
  26.                 std::cout << "[" << boost::this_thread::get_id()  
  27.                     << "] Error: " << ec << std::endl;  
  28.                 global_stream_lock.unlock();  
  29.             }  
  30.             break;  
  31.         }  
  32.         catch( std::exception & ex )  
  33.         {  
  34.             global_stream_lock.lock();  
  35.             std::cout << "[" << boost::this_thread::get_id()  
  36.                 << "] Exception: " << ex.what() << std::endl;  
  37.             global_stream_lock.unlock();  
  38.         }  
  39.     }  
  40.   
  41.     global_stream_lock.lock();  
  42.     std::cout << "[" << boost::this_thread::get_id()  
  43.         << "] Thread Finish" << std::endl;  
  44.     global_stream_lock.unlock();  
  45. }  
  46.   
  47. void TimerHandler(  
  48.                   const boost::system::error_code & error,  
  49.                   boost::shared_ptr< boost::asio::deadline_timer > timer  
  50.                   )  
  51. {  
  52.     if( error )  
  53.     {  
  54.         global_stream_lock.lock();  
  55.         std::cout << "[" << boost::this_thread::get_id()  
  56.             << "] Error: " << error << std::endl;  
  57.         global_stream_lock.unlock();  
  58.     }  
  59.     else  
  60.     {  
  61.         global_stream_lock.lock();  
  62.         std::cout << "[" << boost::this_thread::get_id()  
  63.             << "] TimerHandler " << std::endl;  
  64.         global_stream_lock.unlock();  
  65.   
  66.         timer->expires_from_now( boost::posix_time::seconds( 5 ) );  
  67.         timer->async_wait( boost::bind( &TimerHandler, _1, timer ) );  
  68.     }  
  69. }  
  70.   
  71. int main( int argc, char * argv[] )  
  72. {  
  73.     boost::shared_ptr< boost::asio::io_service > io_service(  
  74.         new boost::asio::io_service  
  75.         );  
  76.     boost::shared_ptr< boost::asio::io_service::work > work(  
  77.         new boost::asio::io_service::work( *io_service )  
  78.         );  
  79.   
  80.     global_stream_lock.lock();  
  81.     std::cout << "[" << boost::this_thread::get_id()  
  82.         << "] Press [return] to exit." << std::endl;  
  83.     global_stream_lock.unlock();  
  84.   
  85.     boost::thread_group worker_threads;  
  86.     forint x = 0; x < 2; ++x )  
  87.     {  
  88.         worker_threads.create_thread( boost::bind( &WorkerThread, io_service ) );  
  89.     }  
  90.   
  91.     boost::shared_ptr< boost::asio::deadline_timer > timer(  
  92.         new boost::asio::deadline_timer( *io_service )  
  93.         );  
  94.     timer->expires_from_now( boost::posix_time::seconds( 5 ) );  
  95.     timer->async_wait( boost::bind( &TimerHandler, _1, timer ) );  
  96.   
  97.     std::cin.get();  
  98.   
  99.     io_service->stop();  
  100.   
  101.     worker_threads.join_all();  
  102.   
  103.     return 0;  
  104. }  

从上面可以发现,通过智能指针结合bind可以将timer反复使用。上面_1是一个占位符,因为TimerHandler需要两个参数,第一个是关于错误码的,第二个是timer类指针。_1表示第一个参数暂时不提供。

         对于上面的例子,如果我们在一个线程中启用timer,在另外一个线程中执行事件,如果timer的handler方法和事件的handler方法需要访问同一个object,那么当这两个事件同时发生时,就会出现线程不安全的情况,怎么避免呢,还是采用strand。

[cpp] view plain copy print?在CODE上查看代码片派生到我的代码片
  1. #include <boost/asio.hpp>   
  2. #include <boost/shared_ptr.hpp>   
  3. #include <boost/thread.hpp>  
  4. #include <boost/thread/mutex.hpp>   
  5. #include <boost/bind.hpp>  
  6. #include <iostream>   
  7.   
  8. boost::mutex global_stream_lock;  
  9.   
  10. void WorkerThread( boost::shared_ptr< boost::asio::io_service > io_service )  
  11. {  
  12.     global_stream_lock.lock();  
  13.     std::cout << "[" << boost::this_thread::get_id()  
  14.         << "] Thread Start" << std::endl;  
  15.     global_stream_lock.unlock();  
  16.   
  17.     whiletrue )  
  18.     {  
  19.         try  
  20.         {  
  21.             boost::system::error_code ec;  
  22.             io_service->run( ec );  
  23.             if( ec )  
  24.             {  
  25.                 global_stream_lock.lock();  
  26.                 std::cout << "[" << boost::this_thread::get_id()   
  27.                     << "] Error: " << ec << std::endl;  
  28.                 global_stream_lock.unlock();  
  29.             }  
  30.             break;  
  31.         }  
  32.         catch( std::exception & ex )  
  33.         {  
  34.             global_stream_lock.lock();  
  35.             std::cout << "[" << boost::this_thread::get_id()  
  36.                 << "] Exception: " << ex.what() << std::endl;  
  37.             global_stream_lock.unlock();  
  38.         }  
  39.     }  
  40.   
  41.     global_stream_lock.lock();  
  42.     std::cout << "[" << boost::this_thread::get_id()  
  43.         << "] Thread Finish" << std::endl;  
  44.     global_stream_lock.unlock();  
  45. }  
  46.   
  47. void TimerHandler(  
  48.                   const boost::system::error_code & error,   
  49.                   boost::shared_ptr< boost::asio::deadline_timer > timer,   
  50.                   boost::shared_ptr< boost::asio::io_service::strand > strand  
  51.                   )  
  52. {  
  53.     if( error )  
  54.     {  
  55.         global_stream_lock.lock();  
  56.         std::cout << "[" << boost::this_thread::get_id()  
  57.             << "] Error: " << error << std::endl;  
  58.         global_stream_lock.unlock();  
  59.     }  
  60.     else  
  61.     {  
  62.         std::cout << "[" << boost::this_thread::get_id()  
  63.             << "] TimerHandler " << std::endl;  
  64.   
  65.         timer->expires_from_now( boost::posix_time::seconds( 1 ) );  
  66.         timer->async_wait(   
  67.             strand->wrap( boost::bind( &TimerHandler, _1, timer, strand ) )  
  68.             );  
  69.     }  
  70. }  
  71.   
  72. void PrintNum( int x )  
  73. {  
  74.     std::cout << "[" << boost::this_thread::get_id()  
  75.         << "] x: " << x << std::endl;  
  76.     boost::this_thread::sleep( boost::posix_time::milliseconds( 1000 ) );  
  77. }  
  78.   
  79. int main( int argc, char * argv[] )  
  80. {  
  81.     boost::shared_ptr< boost::asio::io_service > io_service(  
  82.         new boost::asio::io_service  
  83.         );  
  84.     boost::shared_ptr< boost::asio::io_service::work > work(  
  85.         new boost::asio::io_service::work( *io_service )  
  86.         );  
  87.     boost::shared_ptr< boost::asio::io_service::strand > strand(  
  88.         new boost::asio::io_service::strand( *io_service )  
  89.         );  
  90.   
  91.     global_stream_lock.lock();  
  92.     std::cout << "[" << boost::this_thread::get_id()  
  93.         << "] Press [return] to exit." << std::endl;  
  94.     global_stream_lock.unlock();  
  95.   
  96.     boost::thread_group worker_threads;  
  97.     forint x = 0; x < 2; ++x )  
  98.     {  
  99.         worker_threads.create_thread( boost::bind( &WorkerThread, io_service ) );  
  100.     }  
  101.   
  102.     boost::this_thread::sleep( boost::posix_time::seconds( 1 ) );  
  103.   
  104.     strand->post( boost::bind( &PrintNum, 1 ) );  
  105.     strand->post( boost::bind( &PrintNum, 2 ) );  
  106.     strand->post( boost::bind( &PrintNum, 3 ) );  
  107.     strand->post( boost::bind( &PrintNum, 4 ) );  
  108.     strand->post( boost::bind( &PrintNum, 5 ) );  
  109.   
  110.     boost::shared_ptr< boost::asio::deadline_timer > timer(  
  111.         new boost::asio::deadline_timer( *io_service )  
  112.         );  
  113.     timer->expires_from_now( boost::posix_time::seconds( 1 ) );  
  114.     timer->async_wait(   
  115.         strand->wrap( boost::bind( &TimerHandler, _1, timer, strand ) )  
  116.         );  
  117.   
  118.     std::cin.get();  
  119.   
  120.     io_service->stop();  
  121.   
  122.     worker_threads.join_all();  
  123.   
  124.     return 0;  
  125. }  

运行上面的程序,会发现一个按顺序输出的1 2 3 4 5以及随后输出的TimerHandler,如果没有strand,在输出1 2 3 4 5的过程中可能会出现杂乱的输出,这是因为标准输出std::cout并没有加锁,这就会产生上面说的线程不安全问题,采用strand可以很好的解决这个问题。
0 0
原创粉丝点击