#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的更多相关文章

  1. 【boost】MFC dll中使用boost thread的问题

    项目需要,在MFC dll中使用了boost thread(<boost/thread.hpp>),LoadLibraryEx的时候出现断言错误,去掉thread库引用后断言消失. 百度g ...

  2. boost::thread boost库线程

    一.boost::thread的创建 1.线程创建方法一: boost::shared_ptr<boost::thread> writeThread_; boost::function0& ...

  3. #include <boost/thread.hpp>

    在这个库最重要的一个类就是boost::thread,它是在boost/thread.hpp里定义的,用来创建一个新线程.它已经被纳入C++标准库中. 小结:新一代C++标准将线程库引入后,将简化多线 ...

  4. Boost::Thread使用示例 - CG-Animation - 博客频道 - CSDN.NET

    Boost::Thread使用示例 - CG-Animation - 博客频道 - CSDN.NET Boost::Thread使用示例 分类: C/C++ 2011-07-06 14:48 5926 ...

  5. boost::thread用法

    最近在做一个消息中间件里面涉及到多线程编程,由于跨平台的原因我采用了boost线程库.在创建线程时遇到了几种线程创建方式现总结如下: 首先看看boost::thread的构造函数吧,boost::th ...

  6. Boost::Thread 多线程的基础知识

    Boost.Thread可以使用多线程执行可移植C++代码中的共享数据.它提供了一些类和函数来管理线程本身,还有其它一些为了实现在线程之间同步数据或者提供针对特定单个线程的数据拷贝.头文件:#incl ...

  7. boost::thread类

    前言 标准C++线程即将到来.预言它将衍生自Boost线程库,现在让我们探索一下Boost线程库. 几年前,用多线程执行程序还是一件非比寻常的事.然而今天互联网应用服务程序普遍使用多线程来提高与多客户 ...

  8. Boost::thread库的使用

    阅读对象 本文假设读者有几下Skills [1]在C++中至少使用过一种多线程开发库,有Mutex和Lock的概念. [2]熟悉C++开发,在开发工具中,能够编译.设置boost::thread库. ...

  9. boost::thread之while(true)型线程终结方法

    我们的程序中经常会用到线程来执行某些异步操作,而有些时候我们的线程执行的函数是这个样子的: void ThreadBody() { while( true ) { std::cout << ...

  10. Boost Thread学习笔记五

    多线程编程中还有一个重要的概念:Thread Local Store(TLS,线程局部存储),在boost中,TLS也被称作TSS,Thread Specific Storage.boost::thr ...

随机推荐

  1. c语言下的变量类型及计算

    源码 补码 反码 机器数:一个数在计算机中的二进制表示形式,  叫做这个数的机器数.机器数是带符号的,在计算机用一个数的最高位存放符号, 正数为0, 负数为1.   真值:第一位是符号位,将带符号位的 ...

  2. GDALDataset的创建和销毁

    之前在GDALDestroyDriverManager 分析中没有看到对dGDALDatasert的回收.先看一个例子程序,这个例子打开了一个tif文件,读取了一些基本信息. 为了简单示范,没有写成C ...

  3. 使用ant构建报错,编码GBK的不可映射字符解决方法

    使用ant的核心就是这个build.xml.然后再在cmd中使用ant命令就行了. build.xml构建文件包含一个工程(project). 工程包含若干个目标(target). 目标可以依赖于其他 ...

  4. [Algorithms] Build a Binary Tree in JavaScript and Several Traversal Algorithms

    A binary tree is a tree where each node may only have up to two children. These children are stored ...

  5. automake连载--Linux下使用automake入门

    http://blog.csdn.net/shanzhizi/article/details/30246587 近来重要要总结一下automake的用法了,连载几篇网上已有的文章,以供参考. 作为Li ...

  6. jQuery选取表单元素

    表单元素选择器 选择器                    说明 :button                 <button>元素和type属性值为button的<input& ...

  7. 前端性能优化:DocumentFragments或innerHTML取代复杂的元素注入

    来源:GBin1.com 我们的浏览器执行越来越多的特性,并且网络逐渐向移动设备转移,使我们的前端代码更加紧凑,如何优化,就变得越来越重要了.前端给力的地方是可以有 许多种简单的策略和代码习惯让我们可 ...

  8. Mybatis使用Redis二级缓存

    在Mybatis中允许开发者自定义自己的缓存,本文将使用Redis作为Mybatis的二级缓存.在Mybatis中定义二级缓存,需要如下配置: 1. MyBatis支持二级缓存的总开关:全局配置变量参 ...

  9. java程序main方法的参数String[] args

    public class ArgsTest { public static void main(String[] args) { System.out.println(args.length); fo ...

  10. UISearchbar placeholder 文本和icon居左 iOS7

    在iOS7 下测试了一些方法,发现还是攺变不了文本的居左,最后发现了一个nb的招: _searchBar = [[UISearchBar alloc]initWithFrame:CGRectZero] ...