#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. 关于XUtils框架细解

    感谢关注xUitls的网友最近一段时间给予的热心反馈,xUtils近期做了很多细节优化之后,功能和api已经稳定. 1.9.6主要更新内容:Bitmap加载动画有时重复出现的问题修复,加载过程优化; ...

  2. 【web.xml】报错java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener

    今天搭建新的项目,虽然在web.xml中配置了ContextLoaderListener以及IntrospectorCleanupListener 如下: web.xml中部分代码: <!-- ...

  3. postgres--vacuum

    vacuum的功能 回收空间 数据库总是不断地在执行删除,更新等操作.良好的空间管理非常重要,能够对性能带来大幅提高. postgresql中执行delete操作后,表中的记录只是被标示为删除状态,并 ...

  4. WCF和Socket

    WCF的全称是:Windows Communication Foundation.它是建立在Web Service架构上的一个全新的通信平台.它使用相同的基础结构和 API 来创建应用程序,这些应用程 ...

  5. JVM组成部分以及内存模型

    一.JVM的组成部分 我们先把JVM这个虚拟机实现机制画出来,例如以下图所看到的: 从这个图中能够看到,JVM是执行在操作系统之上的,它与硬件没有直接的交互. 我们再来看下JVM有哪些组 成部分,例如 ...

  6. Echarts 获取后台数据 使用后台数据展示 饼装图

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head& ...

  7. 在SVN中打分支

    项目开发中一个版本号也许就有几次迭代,而完毕一次迭代或一个版本号时,一般会讨论下一步的计划.是否要增加新东西新技术,内部人员是否有变动,这时须要在管理工具中做一些备份,以防我们接下来即使改动serve ...

  8. 图解aclocal、autoconf、automake、autoheader、configure

    http://www.laruence.com/2008/11/11/606.html 本文地址: http://www.laruence.com/2008/11/11/606.html 转载文章 原 ...

  9. 爪哇国新游记之十三----XML文件读写

    /** * XML读写示例 * @author hx * */ public class XmlReaderWriter{ /** * 读取一个XML文件,返回一个雇员链表 * @param file ...

  10. redis学习笔记——命令执行流程

    基础知识部分 如果需要掌握Redis的整个命令的执行过程,那么必须掌握一些基本的概念!否则根本看不懂,下面我就一些在我看来必备的基础知识进行总结,希望能为后面命令的整个执行过程做铺垫. 事件 Redi ...