boost::asio::io_service和io_service::work和boost::thread_group配合使用

来源:互联网 发布:烟台正浩网络老总 编辑:程序博客网 时间:2024/06/05 11:49

boost::asio::io_service和io_service::work和boost::thread_group配合使用

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. //注释中的Page是指:Boost程序库完全开发指南(第三版)  
  2. #include <iostream>  
  3. //Page542: 在头文件<boost/asio.hpp>前定义此宏,它就会向标准流cerr输出运行日志。  
  4. //#define BOOST_ASIO_ENABLE_HANDLER_TRACKING  
  5. #include <boost/asio.hpp>  
  6.   
  7. #include <boost/thread.hpp>    
  8. long boost_thread_id_to_long(boost::thread::id id)  
  9. {  
  10.     std::stringstream ss; ss << id;  
  11.     return strtol(std::string(ss.str()).c_str(), (char**)NULL, 16);  
  12. }  
  13.   
  14. #include <boost/date_time/posix_time/posix_time.hpp>  
  15. std::string local_time_to_simple_string()  
  16. {  
  17.     boost::posix_time::ptime ptm = boost::posix_time::microsec_clock::local_time();  
  18.     return boost::posix_time::to_simple_string(ptm);  
  19. }  
  20.   
  21. #include <boost/format.hpp>  
  22. std::string completion_handler_fun(float f, std::string str)  
  23. {  
  24.     str = (boost::format("%|s|, thread_id=%|ld|, f=%|.3f|, str=%|s|")  
  25.         % local_time_to_simple_string()  
  26.         % boost_thread_id_to_long(boost::this_thread::get_id())  
  27.         % f % str.c_str()).str();  
  28.     std::cout << str << std::endl;  
  29.     return str;  
  30. }  
  31.   
  32. int main1()  
  33. {  
  34.     boost::asio::io_service io;  
  35.     io.post(boost::bind(completion_handler_fun, 3.141592653f, "111"));  
  36.     io.post(boost::bind(completion_handler_fun, 3.141592653f, "222"));  
  37.     std::cout << "sleep some seconds before run" << std::endl;  
  38.     boost::this_thread::sleep_for(boost::chrono::seconds(3));  
  39.     io.run();  
  40.     std::cout << "sleep some seconds after  run" << std::endl;  
  41.     boost::this_thread::sleep_for(boost::chrono::seconds(3));  
  42.     return 0;  
  43.     //在这个程序中,主线程提供了io_service::run的上下文。也可以创建一个线程,然后在这个线程里执行run函数。  
  44.     //当run开始后,就执行所有的handler,如果io_service里面没有可执行的handler了,就退出run函数了。  
  45. }  
  46.   
  47. int main2()  
  48. {  
  49.     boost::asio::io_service io;  
  50.     //请阅读Page540的io_service::work的描述。  
  51.     //io_service::work会让io_service::run始终有事可做,所以不会退出run函数。  
  52.     boost::asio::io_service::work wk(io);  
  53.     io.post(boost::bind(completion_handler_fun, 3.141592653f, "111"));  
  54.     io.post(boost::bind(completion_handler_fun, 3.141592653f, "222"));  
  55.     std::cout << "sleep some seconds before run" << std::endl;  
  56.     boost::this_thread::sleep_for(boost::chrono::seconds(3));  
  57.     io.run();  
  58.     //有io_service::work的存在,所以,即时没有可执行的handler了,也不会退出run函数。  
  59.     std::cout << "io_service::run is end and will exit." << std::endl;  
  60.     return 0;  
  61. }  
  62.   
  63. int main3()  
  64. {  
  65.     boost::asio::io_service io;  
  66.     //事先在io_service里面放了100个要执行的handler。  
  67.     for (int i = 0; i < 100; ++i)  
  68.     {  
  69.         std::stringstream ss; ss << i;  
  70.         io.post(boost::bind(completion_handler_fun, 3.141592653f, std::string(ss.str())));  
  71.     }  
  72.     boost::thread_group th_gp;  
  73.     //线程组创建3个线程,同时处理这100个handler。  
  74.     for (int i = 0; i < 3; ++i)  
  75.     {  
  76.         boost::thread* th_ptr = th_gp.create_thread(boost::bind(&boost::asio::io_service::run, boost::ref(io)));  
  77.         std::cout << "thread_group, thread_id=" << boost_thread_id_to_long(th_ptr->get_id()) << std::endl;  
  78.     }  
  79.     //这3个线程可能同时往屏幕上输出,所以屏幕可能会乱。  
  80.     //执行完handler后,run函数退出,线程退出,thread_group的join_all会执行完毕。  
  81.     th_gp.join_all();  
  82.     std::cout << "will exit" << std::endl;  
  83.     boost::this_thread::sleep_for(boost::chrono::seconds(3));  
  84.     return 0;  
  85. }  
  86.   
  87. int main()  
  88. {  
  89.     boost::asio::io_service io;  
  90.     //io_service::work阻止了io_service的run函数退出,所以thread_group的join_all函数会一直阻塞在那里。  
  91.     boost::asio::io_service::work wk(io);  
  92.     boost::thread_group th_gp;  
  93.     for (int i = 0; i < 3; ++i)  
  94.     {  
  95.         boost::system::error_code error;  
  96.         boost::thread* th_ptr = th_gp.create_thread(boost::bind(&boost::asio::io_service::run, boost::ref(io), error));  
  97.         std::cout << "thread_group, thread_id=" << boost_thread_id_to_long(th_ptr->get_id()) << std::endl;  
  98.     }  
  99.     io.post(boost::bind(completion_handler_fun, 3.141592653f, "xxx"));  
  100.     th_gp.join_all();  
  101.     std::cout << "will exit" << std::endl;  
  102.     boost::this_thread::sleep_for(boost::chrono::seconds(3));  
  103.     return 0;  
  104. }  
0 0
原创粉丝点击