zoukankan      html  css  js  c++  java
  • 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;
    }
    

      

  • 相关阅读:
    DLL内存加载
    Intel汇编程序设计-高级过程(上)
    Intel汇编程序设计-高级过程(上)
    C#-常用
    C#-常用
    C#-MD5
    C#-MD5
    C#-文件操作
    POJ 3320 Jessica's Reading Problem
    POJ 2456 Aggressive cows
  • 原文地址:https://www.cnblogs.com/wucg/p/3868757.html
Copyright © 2011-2022 走看看