首页 > 其他 > 详细

boost thread

时间:2014-07-26 00:59:46      阅读:317      评论:0      收藏:0      [点我收藏+]
#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

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!