boost thread
#include <cassert>
#include <iostream> #include <boost/ref.hpp>
#include <boost/thread.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/atomic.hpp>
#include <boost/bind.hpp> using namespace std;
using namespace boost; void double_int(int & i){
i *= 2;
} int f(int a,int b){
return a+b;
}
int g(int a,int b,int c){
return a+b*c;
} struct A{
int add(int a,int b){
return a+b;
}
}; mutex io_mu; void printing(atomic_int& x, const string& str){
for(int i=0;i<5;++i){
mutex::scoped_lock lock(io_mu);
cout<<this_thread::get_id()<<":"<< str<<++x<<endl;
this_thread::sleep_for(chrono::seconds(1));
}
} int main(){
/*
A a;
int r3 = boost::bind(&A::add, a, 1, 2)();
auto r4 = boost::bind(&A::add, a, _1, 20)(100);
cout<<r3<<endl;
cout<<r4<<endl;
//cout<<typeid(r4).name()<<endl;
*/
/*
int r1 = boost::bind(f,_1,_2)(11,22);
int r2 = boost::bind(g,_3,_2,_1)(10,2,3);
cout<<"r1="<<r1<<",r2="<<r2<<endl;
*/ atomic_int x(0);
auto e1 = boost::bind(printing, boost::ref(x), "hello");
auto e2 = boost::bind(printing, boost::ref(x), "boost"); thread_group tg;
tg.create_thread(e1);
tg.create_thread(e2);
tg.join_all(); /*
atomic_int x(0);
auto e1 = boost::bind(printing, boost::ref(x), "hello");
auto e2 = boost::bind(printing, boost::ref(x), "boost"); thread t1(e1);
thread t2(e2); t1.join();
t2.join();
*/ //this_thread::sleep_for(chrono::seconds(2)); /*
int i = 10;
cout<<i<<endl;
double_int(ref(i));
cout<<i<<endl;
*/
/*
int x = 10;
reference_wrapper<int> rw(x);
assert( x == rw);
(int &)rw = 100;
cout<<"rw="<<rw<<endl;
cout<<"x="<<x<<endl; reference_wrapper<int> rw2(rw);
(int &)rw2 = 101;
cout<<"rw="<<rw<<endl;
cout<<"x="<<x<<endl; auto rw3 = ref(x);
cout<< typeid(rw3).name()<<endl; auto rw4 = cref(x);
cout<< typeid(rw4).name()<<endl;
*/
std::system("pause");
return 0;
}
boost thread的更多相关文章
- 【boost】MFC dll中使用boost thread的问题
项目需要,在MFC dll中使用了boost thread(<boost/thread.hpp>),LoadLibraryEx的时候出现断言错误,去掉thread库引用后断言消失. 百度g ...
- boost::thread boost库线程
一.boost::thread的创建 1.线程创建方法一: boost::shared_ptr<boost::thread> writeThread_; boost::function0& ...
- #include <boost/thread.hpp>
在这个库最重要的一个类就是boost::thread,它是在boost/thread.hpp里定义的,用来创建一个新线程.它已经被纳入C++标准库中. 小结:新一代C++标准将线程库引入后,将简化多线 ...
- Boost::Thread使用示例 - CG-Animation - 博客频道 - CSDN.NET
Boost::Thread使用示例 - CG-Animation - 博客频道 - CSDN.NET Boost::Thread使用示例 分类: C/C++ 2011-07-06 14:48 5926 ...
- boost::thread用法
最近在做一个消息中间件里面涉及到多线程编程,由于跨平台的原因我采用了boost线程库.在创建线程时遇到了几种线程创建方式现总结如下: 首先看看boost::thread的构造函数吧,boost::th ...
- Boost::Thread 多线程的基础知识
Boost.Thread可以使用多线程执行可移植C++代码中的共享数据.它提供了一些类和函数来管理线程本身,还有其它一些为了实现在线程之间同步数据或者提供针对特定单个线程的数据拷贝.头文件:#incl ...
- boost::thread类
前言 标准C++线程即将到来.预言它将衍生自Boost线程库,现在让我们探索一下Boost线程库. 几年前,用多线程执行程序还是一件非比寻常的事.然而今天互联网应用服务程序普遍使用多线程来提高与多客户 ...
- Boost::thread库的使用
阅读对象 本文假设读者有几下Skills [1]在C++中至少使用过一种多线程开发库,有Mutex和Lock的概念. [2]熟悉C++开发,在开发工具中,能够编译.设置boost::thread库. ...
- boost::thread之while(true)型线程终结方法
我们的程序中经常会用到线程来执行某些异步操作,而有些时候我们的线程执行的函数是这个样子的: void ThreadBody() { while( true ) { std::cout << ...
- Boost Thread学习笔记五
多线程编程中还有一个重要的概念:Thread Local Store(TLS,线程局部存储),在boost中,TLS也被称作TSS,Thread Specific Storage.boost::thr ...
随机推荐
- 高并发场景系列(一) 利用redis实现分布式事务锁,解决高并发环境下减库存
原文:http://blog.csdn.net/heyewu4107/article/details/71009712 高并发场景系列(一) 利用redis实现分布式事务锁,解决高并发环境下减库存 问 ...
- Android源代码编译apk导入第三方包报错
报错内容例如以下: make: *** 没有规则能够创建"out/target/common/obj/APPS/ AndroidWFS_intermediates/classes-full- ...
- python staticmethod,classmethod方法的使用和区别以及property装饰器的作用
class Kls(object): def __init__(self, data): self.data = data def printd(self): print(self.data) @st ...
- http://blog.csdn.net/congcong68/article/details/39256307
http://blog.csdn.net/congcong68/article/details/39256307
- LightOJ - 1265 Island of Survival 期望
题目大意:有一个生存游戏,里面t仅仅老虎,d仅仅鹿,另一个人,每天都要有两个生物碰面,如今有下面规则 1.老虎和老虎碰面.两仅仅老虎就会同归于尽 2.老虎和人碰面或者和鹿碰面,老虎都会吃掉对方 3.人 ...
- DirectX游戏开发——从一个小游戏開始
本系列文章由birdlove1987编写,转载请注明出处. 文章链接: http://blog.csdn.net/zhurui_idea/article/details/26364129 写在前面:自 ...
- apache无法启动:The request operation has failed
apache无法启动提示the requested operation has failed 的错误信息,有以下几种解决方法:1.80端口占用 apache默认使用的端口是80,而IIS和迅雷用的也是 ...
- Git学习笔记一--创建版本库、添加文件、提交文件等
Git,是Linus花了两周时间用C写的一个分布式版本控制系统.牛该怎么定义? 其实,很多人都不care谁写了Git,只在乎它是免费而且好用的!So do I! 下面开始我们的学习: 1.Git安装( ...
- 算法笔记_161:算法提高 十进制数转八进制数(Java)
目录 1 问题描述 2 解决方案 1 问题描述 编写函数,其功能为把一个十进制数转换为其对应的八进制数.程序读入一个十进制数,调用该函数实现数制转换后,输出对应的八进制数. 样例输入 9274 样 ...
- Net 服务命令行参考之一
转自:http://blog.sina.com.cn/s/blog_55035e9501015p05.html ----------------------------------最实用高效的网络管理 ...