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;
    }
    

      

  • 相关阅读:
    Python爬取豆瓣电影top
    那些年我们踩的坑,依然有人在踩坑
    工行ICBC_WAPB_B2C支付接口
    SharePoint2016配置工作流开发环境
    Html+Css实现梯形选项卡
    The Ribbon Tab with id: "Ribbon.Read" has not been made available for this page or does not exist.
    SharePoint自动初始化网站列表
    常用的SharePoint命令行代码
    SharePoint开启错误提示
    Asp.Net写入读取Xml(处理文件权限)
  • 原文地址:https://www.cnblogs.com/wucg/p/3868757.html
Copyright © 2011-2022 走看看