【Boost】boost库asio详解1——strand与io_service区别

2024-06-15 01:58

本文主要是介绍【Boost】boost库asio详解1——strand与io_service区别,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

转载于http://blog.csdn.net/huang_xw/article/details/8469851

[cpp]  view plain copy print ?
  1. namespace  
  2. {  
  3.     // strand提供串行执行, 能够保证线程安全, 同时被post或dispatch的方法, 不会被并发的执行.   
  4.     // io_service不能保证线程安全  
  5.     boost::asio::io_service m_service;  
  6.     boost::asio::strand m_strand(m_service);  
  7.     boost::mutex m_mutex;  
  8.   
  9.     void print(int id)  
  10.     {  
  11.         // boost::mutex::scoped_lock lock(m_mutex);  
  12.         static int count = 0;  
  13.         PRINT_DEBUG("id: " << boost::lexical_cast<std::string>(id));  
  14.         PRINT_DEBUG("count: " << boost::lexical_cast<std::string>(++count));  
  15.     }  
  16.       
  17.     void ioRun1()  
  18.     {  
  19.         while(true)  
  20.         {  
  21.             m_service.run();  
  22.         }  
  23.     }  
  24.   
  25.     void ioRun2()  
  26.     {  
  27.         while(true)  
  28.         {  
  29.             m_service.run();  
  30.         }  
  31.     }  
  32.   
  33.     void strand_print1()  
  34.     {  
  35.         // PRINT_DEBUG("Enter print1");  
  36.         m_strand.dispatch(boost::bind(print, 1));  
  37.         // PRINT_DEBUG("Exit print1");  
  38.     }  
  39.   
  40.     void strand_print2()  
  41.     {  
  42.         // PRINT_DEBUG("Enter print2");  
  43.         m_strand.post(boost::bind(print, 2));  
  44.         // PRINT_DEBUG("Exit print2");  
  45.     }  
  46.   
  47.     void strand_print3()  
  48.     {  
  49.         // PRINT_DEBUG("Enter print3");                
  50.         m_strand.post(boost::bind(print, 3));  
  51.         // PRINT_DEBUG("Exit print3");  
  52.     }  
  53.   
  54.     void strand_print4()  
  55.     {  
  56.         // PRINT_DEBUG("Enter print4");  
  57.         m_strand.post(boost::bind(print, 4));  
  58.         // PRINT_DEBUG("Exit print4");  
  59.     }  
  60.   
  61.     // 将上面的m_strand换成m_service后,  
  62.     void service_print1()  
  63.     {  
  64.         // PRINT_DEBUG("Enter print1");  
  65.         m_service.dispatch(boost::bind(print, 1));  
  66.         // PRINT_DEBUG("Exit print1");  
  67.     }  
  68.   
  69.     void service_print2()  
  70.     {  
  71.         // PRINT_DEBUG("Enter print2");  
  72.         m_service.post(boost::bind(print, 2));  
  73.         // PRINT_DEBUG("Exit print2");  
  74.     }  
  75.   
  76.     void service_print3()  
  77.     {  
  78.         // PRINT_DEBUG("Enter print3");                
  79.         m_service.post(boost::bind(print, 3));  
  80.         // PRINT_DEBUG("Exit print3");  
  81.     }  
  82.   
  83.     void service_print4()  
  84.     {  
  85.         // PRINT_DEBUG("Enter print4");  
  86.         m_service.post(boost::bind(print, 4));  
  87.         // PRINT_DEBUG("Exit print4");  
  88.     }  
  89. }  
  90.   
  91. void test_strand()  
  92. {  
  93.     boost::thread ios1(ioRun1);  
  94.     boost::thread ios2(ioRun2);  
  95.       
  96.     boost::thread t1(strand_print1);  
  97.     boost::thread t2(strand_print2);  
  98.     boost::thread t3(strand_print3);  
  99.     boost::thread t4(strand_print4);  
  100.   
  101.     t1.join();  
  102.     t2.join();  
  103.     t3.join();  
  104.     t4.join();  
  105.   
  106.     m_server.run();  
  107. }  
  108.   
  109. void test_service()  
  110. {  
  111.     boost::thread ios1(ioRun1);  
  112.     boost::thread ios2(ioRun2);  
  113.   
  114.     boost::thread t1(service_print1);  
  115.     boost::thread t2(service_print2);  
  116.     boost::thread t3(service_print3);  
  117.     boost::thread t4(service_print4);  
  118.       
  119.     t1.join();  
  120.     t2.join();  
  121.     t3.join();  
  122.     t4.join();  
  123.       
  124.     m_service.run();  
  125. }  
test_strand的执行结果:
[cpp]  view plain copy print ?
  1. 2013-01-05 17:25:34 626 [8228] DEBUG - id: 4  
  2. 2013-01-05 17:25:34 631 [8228] DEBUG - count: 1  
  3. 2013-01-05 17:25:34 634 [5692] DEBUG - id: 1  
  4. 2013-01-05 17:25:34 637 [5692] DEBUG - count: 2  
  5. 2013-01-05 17:25:34 640 [5692] DEBUG - id: 2  
  6. 2013-01-05 17:25:34 642 [5692] DEBUG - count: 3  
  7. 2013-01-05 17:25:34 646 [5692] DEBUG - id: 3  
  8. 2013-01-05 17:25:34 649 [5692] DEBUG - count: 4  
test_ioserivice的执行结果:
[cpp]  view plain copy print ?
  1. 2013-01-05 17:26:28 071 [3236] DEBUG - id: 1  
  2. 2013-01-05 17:26:28 071 [5768] DEBUG - id: 2  
  3. 2013-01-05 17:26:28 071 [5108] DEBUG - id: 3  
  4. 2013-01-05 17:26:28 076 [3236] DEBUG - count: 1  
  5. 2013-01-05 17:26:28 079 [5768] DEBUG - count: 2  
  6. 2013-01-05 17:26:28 083 [5108] DEBUG - count: 3  
  7. 2013-01-05 17:26:28 087 [3236] DEBUG - id: 4  
  8. 2013-01-05 17:26:28 099 [3236] DEBUG - count: 4  
从结果可以看到, 在test_strand中print中两个打印函数成对执行, 在test_ioservice两个打印函数就没有线程安全可言了.
如果要保证test_ioservice同步, 就要加上mutex, 在代码中被注释的那句. 

注意从日志的线程号中可知: 真正执行print()是主线程, ios1, ios2, 而t1, t2, t3, t4线程只是往ioservice的队列中加入任务.

这篇关于【Boost】boost库asio详解1——strand与io_service区别的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1062115

相关文章

MySQL中的分组和多表连接详解

《MySQL中的分组和多表连接详解》:本文主要介绍MySQL中的分组和多表连接的相关操作,本文通过实例代码给大家介绍的非常详细,感兴趣的朋友一起看看吧... 目录mysql中的分组和多表连接一、MySQL的分组(group javascriptby )二、多表连接(表连接会产生大量的数据垃圾)MySQL中的

Java 实用工具类Spring 的 AnnotationUtils详解

《Java实用工具类Spring的AnnotationUtils详解》Spring框架提供了一个强大的注解工具类org.springframework.core.annotation.Annot... 目录前言一、AnnotationUtils 的常用方法二、常见应用场景三、与 JDK 原生注解 API 的

redis中使用lua脚本的原理与基本使用详解

《redis中使用lua脚本的原理与基本使用详解》在Redis中使用Lua脚本可以实现原子性操作、减少网络开销以及提高执行效率,下面小编就来和大家详细介绍一下在redis中使用lua脚本的原理... 目录Redis 执行 Lua 脚本的原理基本使用方法使用EVAL命令执行 Lua 脚本使用EVALSHA命令

SpringBoot3.4配置校验新特性的用法详解

《SpringBoot3.4配置校验新特性的用法详解》SpringBoot3.4对配置校验支持进行了全面升级,这篇文章为大家详细介绍了一下它们的具体使用,文中的示例代码讲解详细,感兴趣的小伙伴可以参考... 目录基本用法示例定义配置类配置 application.yml注入使用嵌套对象与集合元素深度校验开发

Python中的Walrus运算符分析示例详解

《Python中的Walrus运算符分析示例详解》Python中的Walrus运算符(:=)是Python3.8引入的一个新特性,允许在表达式中同时赋值和返回值,它的核心作用是减少重复计算,提升代码简... 目录1. 在循环中避免重复计算2. 在条件判断中同时赋值变量3. 在列表推导式或字典推导式中简化逻辑

Java Stream流使用案例深入详解

《JavaStream流使用案例深入详解》:本文主要介绍JavaStream流使用案例详解,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录前言1. Lambda1.1 语法1.2 没参数只有一条语句或者多条语句1.3 一个参数只有一条语句或者多

SpringBoot整合mybatisPlus实现批量插入并获取ID详解

《SpringBoot整合mybatisPlus实现批量插入并获取ID详解》这篇文章主要为大家详细介绍了SpringBoot如何整合mybatisPlus实现批量插入并获取ID,文中的示例代码讲解详细... 目录【1】saveBATch(一万条数据总耗时:2478ms)【2】集合方式foreach(一万条数

Python装饰器之类装饰器详解

《Python装饰器之类装饰器详解》本文将详细介绍Python中类装饰器的概念、使用方法以及应用场景,并通过一个综合详细的例子展示如何使用类装饰器,希望对大家有所帮助,如有错误或未考虑完全的地方,望不... 目录1. 引言2. 装饰器的基本概念2.1. 函数装饰器复习2.2 类装饰器的定义和使用3. 类装饰

MySQL 中的 JSON 查询案例详解

《MySQL中的JSON查询案例详解》:本文主要介绍MySQL的JSON查询的相关知识,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录mysql 的 jsON 路径格式基本结构路径组件详解特殊语法元素实际示例简单路径复杂路径简写操作符注意MySQL 的 J

Python ZIP文件操作技巧详解

《PythonZIP文件操作技巧详解》在数据处理和系统开发中,ZIP文件操作是开发者必须掌握的核心技能,Python标准库提供的zipfile模块以简洁的API和跨平台特性,成为处理ZIP文件的首选... 目录一、ZIP文件操作基础三板斧1.1 创建压缩包1.2 解压操作1.3 文件遍历与信息获取二、进阶技