#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. Android Parcel对象详解

    关于Parcel的使用 在分析Parcel之前,首先按照分析流程,介绍下关于Parcel的相关常规使用. 首先是关于Parcel的获取: Parcel parcle = Parcel.Obtain() ...

  2. opencv Mat 像素操作

    1 cv::Mat cv::Mat是一个n维矩阵类,声明在<opencv2/core/core.hpp>中.   class CV_EXPORTS Mat { public: //a lo ...

  3. hdu 1244 DP

    水DP dp[i%2][j]=Max(dp[i%2][j-1],dp[1-i%2][j-l[i]]+sum[j]-sum[j-l[i]]); #include "stdio.h" ...

  4. [80Sec]深掘XSS漏洞场景之XSS Rootkit

    顶80SEC的牛. 深掘XSS漏洞场景之XSS Rootkit[完整修订版] EMail: rayh4c#80sec.com Site: http://www.80sec.com Date: 2011 ...

  5. SparkSQL之更改表结构

    本文篇幅较短,内容源于自己在使用SparkSQL时碰到的一个小问题,因为在之后的数据处理过程中多次使用,所以为了加深印象,在此单独成文,以便回顾. 场景 在使用SparkSQL进行数据处理时,碰到这样 ...

  6. 转:windows BAT 批处理脚本教程

    转自:http://www.cnblogs.com/mq0036/p/3412171.html BAT 批处理脚本教程 第一章 批处理基础第一节 常用批处理内部命令简介 批处理定义:顾名思义,批处理文 ...

  7. 省市区县三级联动JAVA+MySQL+JQuery

    场景介绍: 由于项目要求,需要做一个进件(新增)功能,而该功能里要用到车品牌.车系.车型的联动查询,也就是经典的三级联动. 大体思路如下: 进入页面,会把所有的车品牌(第一级)在后台查询出来,放到缓存 ...

  8. java中的序列化和反序列化学习笔记

    须要序列化的Person类: package cn.itcast_07; import java.io.Serializable; /* * NotSerializableException:未序列化 ...

  9. Apple 远程推送APNS 服务

    APP注册APNS流程 http://mobiforge.com/design-development/programming-apple-push-notification-services 服务器 ...

  10. stderr和stdout详细解说(转)

    今天又查了一下fprintf,其中对第一个参数stderr特别感兴趣. int fprintf(FILE *stream,char *format,[argument]): 在此之前先区分一下:pri ...