码迷,mamicode.com
首页 > 其他好文 > 详细

boost thread

时间:2014-07-26 00:59:46      阅读:253      评论:0      收藏:0      [点我收藏+]

标签:blog   os   io   for   re   c   div   amp   

#include <cassert>
#include <iostream>


#include <boost/ref.hpp>
#include <boost/thread.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/atomic.hpp>

using namespace std;
using namespace boost;

void double_int(int & i){
	i *= 2;
}

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<< str<<++x<<endl;
		this_thread::sleep_for(chrono::seconds(1));
	}
}

int main(){
	atomic_int x(0);
	thread(printing, boost::ref(x), "hello");
	thread(printing, boost::ref(x), "boost");
	
	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,布布扣,bubuko.com

boost thread

标签:blog   os   io   for   re   c   div   amp   

原文地址:http://www.cnblogs.com/wucg/p/3868757.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!