Boost-asio之三

来源:互联网 发布:csgo职业选手优化fps 编辑:程序博客网 时间:2024/05/16 11:40
     如果说io_service是Boost.Asio的大脑的话,那么它的成员函数dispatch和post就是它的左臂右膀了。post的主要作用是触发io_service执行post指定的handler并立即返回,post内部实现不会允许io_service调用handler方法。io_service保证handler一定会在调用run,run_one,poll,poll_one的某个线程中被执行。对于dispatch,也是触发io_service执行post指定的handlerio_service也保证handler会在调用run,run_one,poll,poll_one的某个线程中被执行。dispatch相对于post的不同之处在于,dispatch内部实现则允许io_service调用该handler方法,而post禁止。

        也就是说dispatch和post最根本的区别是dispatch本身在条件允许的情况将会立刻执行handler方法,否则就会将其加入到工作队列等待上述四种方法的某一种来触发其执行;而post则总是将任务加入到工作队列。由于handler本身又可能往io_service的工作队列中注入任务,所以如果反复使用dispatch,poll有可能永远不会结束。如果采用post,则会正常结束。

        下面是一个例子用来说明二者的不同:

[cpp] view plain copy print?
  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.     io_service->run();  
  18.   
  19.     global_stream_lock.lock();  
  20.     std::cout << "[" << boost::this_thread::get_id()  
  21.         << "] Thread Finish" << std::endl;  
  22.     global_stream_lock.unlock();  
  23. }  
  24.   
  25. void Dispatch( int x )  
  26. {  
  27.     global_stream_lock.lock();  
  28.     std::cout << "[" <<  boost::this_thread::get_id()  << "] "   
  29.         << __FUNCTION__  << " x = " << x <<  std::endl;  
  30.     global_stream_lock.unlock();  
  31. }  
  32.   
  33. void Post( int x )  
  34. {  
  35.     global_stream_lock.lock();  
  36.     std::cout << "[" <<  boost::this_thread::get_id()  << "] "   
  37.         << __FUNCTION__  << " x = " << x <<  std::endl;  
  38.     global_stream_lock.unlock();  
  39. }  
  40.   
  41. void Run3( boost::shared_ptr< boost::asio::io_service > io_service )  
  42. {  
  43.     forint x = 0; x < 3; ++x )  
  44.     {  
  45.         io_service->dispatch( boost::bind( &Dispatch, x * 2 ) );  
  46.         io_service->post( boost::bind( &Post, x * 2 + 1 ) );  
  47.         boost::this_thread::sleep( boost::posix_time::milliseconds( 1000 ) );  
  48.     }  
  49. }  
  50.   
  51. int main( int argc, char * argv[] )  
  52. {  
  53.     boost::shared_ptr< boost::asio::io_service > io_service(   
  54.         new boost::asio::io_service  
  55.     );  
  56.     boost::shared_ptr< boost::asio::io_service::work > work(  
  57.         new boost::asio::io_service::work( *io_service )  
  58.     );  
  59.   
  60.     global_stream_lock.lock();  
  61.     std::cout << "[" <<  boost::this_thread::get_id()    
  62.         << "] The program will exit when all  work has finished." <<  std::endl;  
  63.     global_stream_lock.unlock();  
  64.   
  65.     boost::thread_group worker_threads;  
  66.     forint x = 0; x < 1; ++x )  
  67.     {  
  68.         worker_threads.create_thread( boost::bind( &WorkerThread, io_service ) );  
  69.     }  
  70.   
  71.     io_service->post( boost::bind( &Run3, io_service ) );  
  72.   
  73.     work.reset();  
  74.   
  75.     worker_threads.join_all();  
  76.   
  77.     return 0;  
  78. }  
        我们发现,输出没有按照顺序输出,这是因为只有一个线程,dispatch在此时不管工作队列中有多少任务都会优先执行,而post则要等待队列中的其它完成后再执行;如果线程改为2个或更多,就是顺序输出了,这是因为有sleep的存在,去掉sleep后输出取决于谁先获得锁。如果工作有先后顺序,采用上面的方法显然是不能达到预期的。

        解决上面的问题,boost提供了strand来给工作排序。strand能够保证handler有序执行,也就是说,如果我们通过strand给io_services post了work1->work2-->work3,不管有多少线程,它们都会按照那个顺序执行。下面是strand使用的一些规则,这对使用strand至关重要,如果我们不太了解,编写的代码可能会产生未定义的行为,导致运行很长时间,有时程序崩溃也很难排查。

规则:

如果有:strand s;满足完成处理要求的对象a,b;a,b的拷贝a1,b1;

当下面的任何之一条件成立:

  • s.post(a)早于s.post(b)
  • s.post(a)早于s.dispatch(b),后者在strand外进行
  • s.dispatch(a)早于s.post(b),前者在strand外进行
  • s.dispatch(a)早于s.dispatch(b),两者均在strand外进行

那么:asio_handler_invoke(a1,&a1)早于asio_handler_invoke(b1,&b1)

但是,如果像下面的情况:

async_op_1(..., s.wrap( a ));
async_op_2(..., s.wrap( b ));

此时,这两个异步操作都会调用对应的dispatch,但是我们不知道dispatch(a)和dispach(b)的顺序。也就是说,上面任何一个条件都不满足,此时就不能决定二者的顺序

        下面是例子,注意,去掉了标准输出上的锁,采用多线程:

[cpp] view plain copy print?
  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() << "] Thread Start" << std::endl;  
  14.     global_stream_lock.unlock();  
  15.   
  16.     io_service->run();  
  17.   
  18.     global_stream_lock.lock();  
  19.     std::cout << "[" << boost::this_thread::get_id()  
  20.         << "] Thread Finish" << std::endl;  
  21.     global_stream_lock.unlock();  
  22. }  
  23.   
  24. void PrintNum( int x )  
  25. {  
  26.     std::cout << "[" << boost::this_thread::get_id()   
  27.         << "] x: " << x << std::endl;  
  28. }  
  29.   
  30. int main( int argc, char * argv[] )  
  31. {  
  32.     boost::shared_ptr< boost::asio::io_service > io_service(  
  33.         new boost::asio::io_service  
  34.     );  
  35.     boost::shared_ptr< boost::asio::io_service::work > work(  
  36.         new boost::asio::io_service::work( *io_service )  
  37.     );  
  38.     boost::asio::io_service::strand strand( *io_service );  
  39.   
  40.     global_stream_lock.lock();  
  41.     std::cout << "[" <<  boost::this_thread::get_id()    
  42.         << "] The program will exit when all  work has finished." <<  std::endl;  
  43.     global_stream_lock.unlock();  
  44.   
  45.     boost::thread_group worker_threads;  
  46.     forint x = 0; x < 4; ++x )  
  47.     {  
  48.         worker_threads.create_thread( boost::bind( &WorkerThread, io_service ) );  
  49.     }  
  50.   
  51.     boost::this_thread::sleep( boost::posix_time::milliseconds( 100 ) );  
  52.     io_service->post( strand.wrap( boost::bind( &PrintNum, 1 ) ) );  
  53.     io_service->post( strand.wrap( boost::bind( &PrintNum, 2 ) ) );  
  54.   
  55.     boost::this_thread::sleep( boost::posix_time::milliseconds( 100 ) );  
  56.     io_service->post( strand.wrap( boost::bind( &PrintNum, 3 ) ) );  
  57.     io_service->post( strand.wrap( boost::bind( &PrintNum, 4 ) ) );  
  58.   
  59.     boost::this_thread::sleep( boost::posix_time::milliseconds( 100 ) );  
  60.     io_service->post( strand.wrap( boost::bind( &PrintNum, 5 ) ) );  
  61.     io_service->post( strand.wrap( boost::bind( &PrintNum, 6 ) ) );  
  62.   
  63.     work.reset();  
  64.   
  65.     worker_threads.join_all();  
  66.   
  67.     return 0;  
  68. }  
上面的strand并不能保证按需输出,改为
[cpp] view plain copy print?
  1. strand.post( boost::bind( &PrintNum, 1 ) );  
等形式就可以实现了。也就是说,在多线程中,如果我们采用下面的A类方法,可以保证顺序,采用B,则不能;这在strand的介绍文档中有专门的说明,是因为strand的post方法保序,而strand wrap到某个方法上并不能保证整体有序。

[cpp] view plain copy print?在CODE上查看代码片派生到我的代码片
  1. A:  
  2. strand.post( boost::bind( &PrintNum, 1 ) );  
  3. strand.post( boost::bind( &PrintNum, 2 ) );  
  4. strand.post( boost::bind( &PrintNum, 3 ) );  
  5.    
  6. B:  
  7. io_service->post( strand.wrap( boost::bind( &PrintNum, 1) ) );  
  8. io_service->post( strand.wrap( boost::bind( &PrintNum, 2 ) ) );  
  9. io_service->post( strand.wrap( boost::bind( &PrintNum, 3 ) ) );  
0 0
原创粉丝点击