http://www.gamedev.net/blog/950/entry-2249317-a-guide-to-getting-started-with-boostasio?pg=5

4. Serializing our workload with strand
使用strand将任务排序
即使在多线程情况下,我们也希望任务能按照post的次序执行

// 1111.cpp : 定义控制台应用程序的入口点。
//
#include <boost/asio.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/thread.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/bind.hpp>
#include <iostream>

boost::mutex global_stream_lock;

void WorkerThread(boost::shared_ptr< boost::asio::io_service > io_service)
{
global_stream_lock.lock();
std::cout << "[" << boost::this_thread::get_id()
<< "] Thread Start" << std::endl;
global_stream_lock.unlock();

io_service->run();

global_stream_lock.lock();
std::cout << "[" << boost::this_thread::get_id()
<< "] Thread Finish" << std::endl;
global_stream_lock.unlock();
}

void PrintNum(int x)
{
std::cout << "[" << boost::this_thread::get_id()
<< "] x: " << x << std::endl;
}

int main(int argc, char * argv[])
{
boost::shared_ptr< boost::asio::io_service > io_service(
new boost::asio::io_service
);
boost::shared_ptr< boost::asio::io_service::work > work(
new boost::asio::io_service::work(*io_service)
);
boost::asio::io_service::strand strand(*io_service);

global_stream_lock.lock();
std::cout << "[" << boost::this_thread::get_id()
<< "] The program will exit when all work has finished." << std::endl;
global_stream_lock.unlock();

boost::thread_group worker_threads;
for (int x = 0; x < 2; ++x)
{
worker_threads.create_thread(boost::bind(&WorkerThread, io_service));
}

boost::this_thread::sleep(boost::posix_time::milliseconds(1000));
/*
strand.post( boost::bind( &PrintNum, 1 ) );
strand.post( boost::bind( &PrintNum, 2 ) );
strand.post( boost::bind( &PrintNum, 3 ) );
strand.post( boost::bind( &PrintNum, 4 ) );
strand.post( boost::bind( &PrintNum, 5 ) );
*/

io_service->post(boost::bind(&PrintNum, 1));
io_service->post(boost::bind(&PrintNum, 2));
io_service->post(boost::bind(&PrintNum, 3));
io_service->post(boost::bind(&PrintNum, 4));
io_service->post(boost::bind(&PrintNum, 5));

work.reset();

worker_threads.join_all();

return 0;
}
代码中开启了两个线程运行io_service的run函数
未使用strand来post任务时候 显示是不确定的
而使用了strand后 显示是按照输入的次序依次显示、

 // deleteMe.cpp : 定义控制台应用程序的入口点。
// #include "stdafx.h"
#include <boost/asio.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/thread.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/bind.hpp>
#include <iostream> boost::mutex global_stream_lock; void WorkerThread(boost::shared_ptr< boost::asio::io_service > io_service)
{
global_stream_lock.lock();
std::cout << "[" << boost::this_thread::get_id() << "] Thread Start" << std::endl;
global_stream_lock.unlock(); io_service->run(); global_stream_lock.lock();
std::cout << "[" << boost::this_thread::get_id()
<< "] Thread Finish" << std::endl;
global_stream_lock.unlock();
} void PrintNum(int x)
{
std::cout << "[" << boost::this_thread::get_id()
<< "] x: " << x << std::endl;
} int main(int argc, char * argv[])
{
boost::shared_ptr< boost::asio::io_service > io_service(
new boost::asio::io_service
);
boost::shared_ptr< boost::asio::io_service::work > work(
new boost::asio::io_service::work(*io_service)
);
boost::asio::io_service::strand strand(*io_service); global_stream_lock.lock();
std::cout << "[" << boost::this_thread::get_id()
<< "] The program will exit when all work has finished." << std::endl;
global_stream_lock.unlock(); boost::thread_group worker_threads;
for (int x = ; x < ; ++x)
{
worker_threads.create_thread(boost::bind(&WorkerThread, io_service));
} boost::this_thread::sleep(boost::posix_time::milliseconds());
io_service->post(strand.wrap(boost::bind(&PrintNum, )));
io_service->post(strand.wrap(boost::bind(&PrintNum, ))); boost::this_thread::sleep(boost::posix_time::milliseconds());
io_service->post(strand.wrap(boost::bind(&PrintNum, )));
io_service->post(strand.wrap(boost::bind(&PrintNum, ))); boost::this_thread::sleep(boost::posix_time::milliseconds());
io_service->post(strand.wrap(boost::bind(&PrintNum, )));
io_service->post(strand.wrap(boost::bind(&PrintNum, ))); work.reset(); worker_threads.join_all(); return ;
}

boost asio 学习(四)使用strand将任务排序的更多相关文章

  1. boost asio 学习(一)io_service的基础

    原文  http://www.gamedev.net/blog/950/entry-2249317-a-guide-to-getting- started-with-boostasio/ 编译环境 b ...

  2. boost asio 学习(九) boost::asio 网络封装

    http://www.gamedev.net/blog/950/entry-2249317-a-guide-to-getting- started-with-boostasio?pg=10 9. A ...

  3. boost asio 学习(八) 网络基础 二进制写发送和接收

    http://www.gamedev.net/blog/950/entry-2249317-a-guide-to-getting- started-with-boostasio?pg=9 8. Net ...

  4. boost asio 学习(七) 网络基础 连接器和接收器(TCP示例)

    http://www.gamedev.net/blog/950/entry-2249317-a-guide-to-getting- started-with-boostasio?pg=8 7. Net ...

  5. boost asio 学习(六) 定时器

    http://www.gamedev.net/blog/950/entry-2249317-a-guide-to-getting- started-with-boostasio?pg=7 6 定时器 ...

  6. BOOST ASIO 学习专贴

    本文已于20170903更新完毕,所有boost asio 代码均为本人手抄.编译器为vs2013,并且所有代码已经上传,本文下方可下载源码 为了学习boost asio库,我是从boost的官方bo ...

  7. boost::asio 学习

    安装 下载-解压 指定安装目录 ./bootstrap.sh --prefix=/usr/local/boost_1_68_0 查看所有必须要编译才能使用的库 ./b2 --show-librarie ...

  8. boost asio 学习(五) 错误处理

    http://www.gamedev.net/blog/950/entry-2249317-a-guide-to-getting-started-with-boostasio?pg=6 5. Erro ...

  9. boost asio 学习(三)post与dispatch

    http://www.gamedev.net/blog/950/entry-2249317-a-guide-to-getting-started-with-boostasio?pg=4 本章节为io_ ...

随机推荐

  1. 爬虫之PyQuery

    PyQuery 是 Python 仿照 jQuery 的严格实现.语法与 jQuery 几乎完全相同. 官方文档:http://pyquery.readthedocs.io/ 安装 pip insta ...

  2. 我是一个录像机(NVR)

    我是一个网络录像机,简称NVR.我的前辈是DVR,我们的区别很简单,DVR接的是模拟摄像机,我连接的是IP摄像机. 我的前辈DVR比我辛苦,因为模拟摄像机的模拟信号连过来之后,他要进行数字化.编码压缩 ...

  3. [蓝桥杯]ALGO-185.算法训练_Trash Removal

    题目描述: 代码如下: #include <algorithm> #include <cstdio> #include <cstdlib> #include < ...

  4. python:数据类型dict

    一.字典 key -->value 储存大量数据,而且是关系型数据,查询速度非常快 数据类型分类: 可变数据类型:list , dict, set 不可变的数据类型:int , bool, st ...

  5. FragmentTabHost切换Fragment时保存状态,避免切换Fragment走onCreateView和onDestroyView方法;

    FragmentTabHost这个控件每次切换Fragment,都会走Fragment的onCreateView和onDestroyView方法,多以每次切换都会创建和销毁Fragment实例,先来看 ...

  6. Python全栈开发记录_第三篇(linux(ubuntu)的操作)

    该篇幅主要记录linux的操作,常见就不记录了,主要记录一些不太常用.难用或者自己忘记了的点. 看到https://www.cnblogs.com/resn/p/5800922.html这篇幅讲解的不 ...

  7. leetCode27.移除元素

    给定一个数组 nums 和一个值 val,你需要原地移除所有数值等于 val 的元素,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成 ...

  8. delete content on the right of cursor, Mac

    delete content on the right of cursor, Mac It's not convenient to press Fn+delete to delete content ...

  9. Open SuSE 安装Python3.6

    1. 下载Python3.6 tar包 去除Modules/Setup文件167行的注释 readline readline.c -lreadline -ltermcap 2. 下载readline- ...

  10. Spring中Bean及@Bean的理解

    Spring中Bean及@Bean的理解 Bean在Spring和SpringMVC中无所不在,将这个概念内化很重要,下面分享一下我的想法: 一.Bean是啥 1.Java面向对象,对象有方法和属性, ...