namespace
{// strand提供串行执行, 能够保证线程安全, 同时被post或dispatch的方法, 不会被并发的执行. // io_service不能保证线程安全boost::asio::io_service m_service;boost::asio::strand m_strand(m_service);boost::mutex m_mutex;void print(int id){// boost::mutex::scoped_lock lock(m_mutex);static int count = 0;PRINT_DEBUG("id: " << boost::lexical_cast<std::string>(id));PRINT_DEBUG("count: " << boost::lexical_cast<std::string>(++count));}void ioRun1(){while(true){m_service.run();}}void ioRun2(){while(true){m_service.run();}}void strand_print1(){// PRINT_DEBUG("Enter print1");m_strand.dispatch(boost::bind(print, 1));// PRINT_DEBUG("Exit print1");}void strand_print2(){// PRINT_DEBUG("Enter print2");m_strand.post(boost::bind(print, 2));// PRINT_DEBUG("Exit print2");}void strand_print3(){// PRINT_DEBUG("Enter print3");              m_strand.post(boost::bind(print, 3));// PRINT_DEBUG("Exit print3");}void strand_print4(){// PRINT_DEBUG("Enter print4");m_strand.post(boost::bind(print, 4));// PRINT_DEBUG("Exit print4");}// 将上面的m_strand换成m_service后,void service_print1(){// PRINT_DEBUG("Enter print1");m_service.dispatch(boost::bind(print, 1));// PRINT_DEBUG("Exit print1");}void service_print2(){// PRINT_DEBUG("Enter print2");m_service.post(boost::bind(print, 2));// PRINT_DEBUG("Exit print2");}void service_print3(){// PRINT_DEBUG("Enter print3");              m_service.post(boost::bind(print, 3));// PRINT_DEBUG("Exit print3");}void service_print4(){// PRINT_DEBUG("Enter print4");m_service.post(boost::bind(print, 4));// PRINT_DEBUG("Exit print4");}
}void test_strand()
{boost::thread ios1(ioRun1);boost::thread ios2(ioRun2);boost::thread t1(strand_print1);boost::thread t2(strand_print2);boost::thread t3(strand_print3);boost::thread t4(strand_print4);t1.join();t2.join();t3.join();t4.join();m_server.run();
}void test_service()
{boost::thread ios1(ioRun1);boost::thread ios2(ioRun2);boost::thread t1(service_print1);boost::thread t2(service_print2);boost::thread t3(service_print3);boost::thread t4(service_print4);t1.join();t2.join();t3.join();t4.join();m_service.run();
}

test_strand的执行结果:
2013-01-05 17:25:34 626 [8228] DEBUG - id: 4
2013-01-05 17:25:34 631 [8228] DEBUG - count: 1
2013-01-05 17:25:34 634 [5692] DEBUG - id: 1
2013-01-05 17:25:34 637 [5692] DEBUG - count: 2
2013-01-05 17:25:34 640 [5692] DEBUG - id: 2
2013-01-05 17:25:34 642 [5692] DEBUG - count: 3
2013-01-05 17:25:34 646 [5692] DEBUG - id: 3
2013-01-05 17:25:34 649 [5692] DEBUG - count: 4

test_ioserivice的执行结果:
2013-01-05 17:26:28 071 [3236] DEBUG - id: 1
2013-01-05 17:26:28 071 [5768] DEBUG - id: 2
2013-01-05 17:26:28 071 [5108] DEBUG - id: 3
2013-01-05 17:26:28 076 [3236] DEBUG - count: 1
2013-01-05 17:26:28 079 [5768] DEBUG - count: 2
2013-01-05 17:26:28 083 [5108] DEBUG - count: 3
2013-01-05 17:26:28 087 [3236] DEBUG - id: 4
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的队列中加入任务.

--------------------- 
原文:https://blog.csdn.net/huang_xw/article/details/8469851

【Boost】boost库asio详解1——strand与io_service区别相关推荐

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

    [cpp] view plain copy  print? namespace { // strand提供串行执行, 能够保证线程安全, 同时被post或dispatch的方法, 不会被并发的执行. ...

  2. 【Boost】boost库asio详解7——boost::asio::buffer用法

    1. asio::buffer常用的构造方法 asio::buffer有多种的构造方法,而且buffer大小是自动管理的 1.1 字符数组 [cpp] view plain copy  print? ...

  3. 【Boost】boost库asio详解8——TCP的简单例子1

    摘于boost官网的几个例子, 做了点小修改, 笔记之. 同步客户端 [cpp] view plain copy  print? void test_asio_synclient() { typede ...

  4. 【Boost】boost库asio详解2——io_service::run函数无任务时退出的问题

    io_service::work类可以使io_service::run函数在没有任务的时候仍然不返回,直至work对象被销毁. [cpp] view plaincopy print? void tes ...

  5. 【Boost】boost库asio详解9——UDP的简单例子1

    服务器: #include "stdafx.h" #include <iostream> #include <boost/asio.hpp> #includ ...

  6. 【Boost】boost库asio详解9——TCP的简单例子2

    客户端: // Client.cpp : 定义控制台应用程序的入口点. //#include "stdafx.h" #include <iostream> #inclu ...

  7. 【Boost】boost库asio详解6——boost::asio::error的用法浅析

    1. 概述 一般而言我们创建用于接收error的类型大多声明如下: [cpp] view plain copy  print? boost::system::error_code error 我们用这 ...

  8. 【Boost】boost库asio详解5——resolver与endpoint使用说明

    tcp::resolver一般和tcp::resolver::query结合用,通过query这个词顾名思义就知道它是用来查询socket的相应信息,一般而言我们关心socket的东东有address ...

  9. 【Boost】boost库asio详解4——deadline_timer使用说明

    deadline_timer和socket一样,都用io_service作为构造函数的参数.也即,在其上进行异步操作,都将导致和io_service所包含的iocp相关联.这同样意味着在析构 io_s ...

最新文章

  1. mongo数据库数据迁移到muysql数据库
  2. Boost::context模块fiber的jump测试程序
  3. 希望我不会“伤心至死”
  4. 架构师进阶之独孤九剑:设计模式详解
  5. SpringMVC 全局异常处理的简单应用
  6. k均值的损失函数_K-Means算法的实现
  7. Python+django网页设计入门(9):自定义反爬虫功能
  8. 玩转基金(1)基金基础
  9. matlab求带参数二重定积分,matlab二重定积分
  10. 解决IOS浏览器或者微信浏览器播放audio音效第二次播放不全
  11. python moving average_Python实现滑动平均(Moving Average)的代码教程
  12. 苹果app退款_苹果 App Store 已购买的应用如何申请退款?
  13. ARP协议及局域网断网攻击(scapy)
  14. 多个PDF文件合并方法
  15. HTML实现怀旧小游戏,超级玛丽、飞机大战…等十余款【完整源码分享】
  16. 普通人如何应对经济危机
  17. 前端检测图片加载失败,替换图片
  18. DOM DOM概述
  19. 利用教育邮箱注册JetBrains产品(pycharm、idea等)的方法
  20. 读论文—基于统计模型改进Word2vec优化策略研究

热门文章

  1. 道德经全文及译文 第二章
  2. HTML5期末大作业:网站——个人网站介绍 (7页面带轮播特效)明星赵丽颖 学生DW网页设计作业源码 web课程设计网页规划与设计 大学生个人网站作业模板
  3. 通过计算两个句子的相似度进行文件命批量修改
  4. python 数据处理1-----对文件下的所有文件批量重命名
  5. Unity开启摄像头并铺满屏幕
  6. ICRA2021| Intensity-SLAM:基于强度辅助的大规模环境定位和建图
  7. 离开互联网上岸1年后,后悔了!重回大厂内卷
  8. 制造业数字化转型的意义
  9. android+手机流量超出自动关闭数据流量功能代码实现,iPhone手机居然偷跑流量?!赶紧关闭这四个功能吧!...
  10. 在线支付系列【18】微信支付实战篇之集成账单相关API