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 ...
随机推荐
- 给XC2440开发板烧写程序的N种方式
转:http://blog.chinaunix.net/uid-22030783-id-3420080.html 给XC2440开发板烧写程序非常灵活,总结起来有这么几种方式: 空片烧写(flas ...
- DN安卓2014版(5-9)
DN安卓2014版(5-9) 联系2g32@sina.com
- Oracle 11gR2 RAC 数据库不能连接(ORA-12537: TNS:connection closed)
Oracle 11gR2 RAC 数据库不能连接(ORA-12537: TNS:connection closed)的解决 [oracle@rac01 ~]$ sqlplus /nolog SQL*P ...
- Android 卡顿优化 4 布局优化实际技巧
今天分享一些layout布局书写中的一些技巧,希望看过之后你也一样可以写出性价比高的布局.我个人的目标是用最少的View写出一样效果的布局.因为我相信View的数量减少伴随着的就是层级的减少.从而达到 ...
- sudoers文件属性的修改导致sudo命令失效
1.sudoers文件属性为440 -r--r-----. 1 root root 4033 Dec 28 18:57 sudoers 2.如果修改sudoers文件属性,就会导致sudo命令不可用, ...
- ylbtech-LanguageSamples-OfficeSample(COM 互操作)
ylbtech-Microsoft-CSharpSamples:ylbtech-LanguageSamples-OfficeSample(COM 互操作) 1.A,示例(Sample) 返回顶部 本示 ...
- TensorFlow------TFRecords的分析与存储实例
TensorFlow------TFRecords的分析与存储实例: import os import tensorflow as tf # 定义cifar的数据等命令行参数 FLAGS = tf.a ...
- Add Two Numbers(from leetcode python 链表)
给定两个非空链表来表示两个非负整数.位数按照逆序方式存储,它们的每个节点只存储单个数字.将两数相加返回一个新的链表. 你可以假设除了数字 0 之外,这两个数字都不会以零开头. 示例: 输入:(2 -& ...
- TestNG 四 测试方法之工厂
工厂允许你动态的创建测试.例如,假设你需要创建一个测试方法,并用它来多次访问一个web页面,而且每次都带有不同的参数: public class TestWebServer { @Test(param ...
- RTP Tools
RTP Tools (Version 1.20) https://wiki.wireshark.org/RTP_statistics Here is a small example: Install ...