【Boost】boost库asio详解2——strand与io_service区别
- 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的队列中加入任务.
【Boost】boost库asio详解2——strand与io_service区别相关推荐
- 【Boost】boost库asio详解1——strand与io_service区别
namespace {// strand提供串行执行, 能够保证线程安全, 同时被post或dispatch的方法, 不会被并发的执行. // io_service不能保证线程安全boost::asi ...
- 【Boost】boost库asio详解7——boost::asio::buffer用法
1. asio::buffer常用的构造方法 asio::buffer有多种的构造方法,而且buffer大小是自动管理的 1.1 字符数组 [cpp] view plain copy print? ...
- 【Boost】boost库asio详解8——TCP的简单例子1
摘于boost官网的几个例子, 做了点小修改, 笔记之. 同步客户端 [cpp] view plain copy print? void test_asio_synclient() { typede ...
- 【Boost】boost库asio详解2——io_service::run函数无任务时退出的问题
io_service::work类可以使io_service::run函数在没有任务的时候仍然不返回,直至work对象被销毁. [cpp] view plaincopy print? void tes ...
- 【Boost】boost库asio详解9——UDP的简单例子1
服务器: #include "stdafx.h" #include <iostream> #include <boost/asio.hpp> #includ ...
- 【Boost】boost库asio详解9——TCP的简单例子2
客户端: // Client.cpp : 定义控制台应用程序的入口点. //#include "stdafx.h" #include <iostream> #inclu ...
- 【Boost】boost库asio详解6——boost::asio::error的用法浅析
1. 概述 一般而言我们创建用于接收error的类型大多声明如下: [cpp] view plain copy print? boost::system::error_code error 我们用这 ...
- 【Boost】boost库asio详解5——resolver与endpoint使用说明
tcp::resolver一般和tcp::resolver::query结合用,通过query这个词顾名思义就知道它是用来查询socket的相应信息,一般而言我们关心socket的东东有address ...
- 【Boost】boost库asio详解4——deadline_timer使用说明
deadline_timer和socket一样,都用io_service作为构造函数的参数.也即,在其上进行异步操作,都将导致和io_service所包含的iocp相关联.这同样意味着在析构 io_s ...
最新文章
- linux centos 丢失 grub.conf 启动系统
- conv2d函数_Pytorch 从0开始学(6)——Conv2d 详解
- Dockerfile项目环境介绍
- 获取族_批量添加族参数(上)
- Google 的开源技术protobuf 简介与例子
- Qt工作笔记-重写滚轮事件,实现界面的增加、减少(放大、缩小)
- 为什么优酷站点限制不了_什么是站点可靠性工程师,为什么要考虑这个职业道路
- 【codevs1226】倒水问题,BFS练习
- NodeJS服务器退出:完成任务,优雅退出
- android游戏勿扰,App+1 | Android 勿扰自动化,看剧、游戏更「沉浸」:NoPopping
- ARP欺骗-教程详解
- netperf 学习笔记
- 车联网信息服务数据——采集合规性——行业标准解读
- 盘点国外知名量化基金
- UISlider实现背景图
- PTA-IP地址转换
- 教你如何用python制作平面直角坐标系模拟器 python项目小发明 【安安教具】-【数学】-【平面直角坐标系】模拟器
- 人物画像————圆球转动效果
- 【FPGA知识点】八段共阳极数码管编码表
- 数据应用案例之“客户画像体系”
热门文章
- 默认标准错误文件linux,Linux中标准输出和标准错误的重导向
- Java:ThreadPoolExecutor解析续--Executors
- 天津市职高高一计算机试题及答案,职高(中职)数学(基础模块)上册题库.doc
- android中调用fft函数,J使用PCM数据在Android中转换FFT(JTransforms FFT in Android from PCM data)...
- 解决 Tomcat 下 getInitParameter 返回 null
- CloudCare容器技术白皮书
- 苦酒入喉心作痛,红酒入鹅鹅想哭——震惊!勒索病毒想哭靠wine感染了Ubuntu16.04...
- Redis的数据类型之String
- MySQL之mysql客户端工作的批处理一些使用手法
- Mac下crontab -e没结果的解决办法