C++ Boost 多线程(二),线程的参数传递

#include <iostream>
#include <boost/thread.hpp>

using namespace std;

void func1(const int &id)
{
	cout<<"func1 id : "<<id<<endl;
}

void func2(const int &id)
{
	cout<<"func2 id : "<<id<<endl;
}

void func3(const int &id)
{
	cout<<"func3 id : "<<id<<endl;
}

//线程的参数传递
int main()
{

	boost::thread t1(func1, 11);
	boost::thread t2(func2, 22);
	boost::thread t3(func3, 33);
	t1.join();
	t2.join();
	t3.join();

	system("pause");
	return 0;
}
#include <iostream>
#include <boost/thread.hpp>

using namespace std;

void func1(const int &id)
{
	cout<<"func1 id : "<<id<<endl;
}

struct MyThread
{
	void operator()(const int &id)
	{
		cout<<"MyThread id : "<<id<<endl;
	}

	void func1(const int &id)
	{
		cout<<"MyThread::func1 id : "<<id<<endl;
	}
};


//线程参数的传递方式
int main()
{
	//普通函数
	boost::thread t1(func1, 11);
	t1.join();

	//函数对象
	MyThread myThread;
	boost::thread t2(myThread, 22);
	t2.join();

	//成员函数
	boost::thread t3(&MyThread::func1, myThread, 33);
	t3.join();

	//临时对象
	boost::thread t4(MyThread(), 44);
	t4.join();

	//对象引用
	boost::thread t5(boost::ref(myThread), 55);
	t5.join();

	system("pause");
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值