C/C++基础,Boost创建线程、线程同步

Boost::thread构建线程的方法:

(1)thread():构造一个表示当前执行线程的线程对象; 
(2)explicit thread(const boost::function0<void>& threadfunc): 
     boost::function0<void>可以简单看为:一个无返回(返回void),无参数的函数。这里的函数也可以是类重载operator()构成的函数;该构造函数传入的是函数对象而并非是函数指针,这样一个具有一般函数特性的类也能作为参数传入。

方式一:全局函数创建线程

#include <iostream>
#include <boost/thread.hpp>
#include <boost/bind.hpp>
//Linux make: g++ -o main main8.c -lboost_system -lboost_thread

using namespace boost;

void thread1(int x){
	std::cout << "x= " << x << std::endl;
	for (int i = 0; i < 5; i++){
		std::cout << "thread1: " << i << std::endl;
	}
}
void thread2(int x){
	std::cout << "x= " << x << std::endl;
	for (int i = 0; i < 5; i++){
		std::cout << "thread2: " << i << std::endl;
	}
}

int main(){
	//boost::thread thrd1(&thread1, 2);//线程中传入函数thrd1 地址,并传入参数2
	//boost::thread thrd2(&thread2, 3);
	boost::thread thrd1(boost::bind(&thread1, 1));//使用boost::bind()函数,实现函数绑定
	boost::thread thrd2(boost::bind(&thread2, 2));
	thrd1.join();
	thrd2.join();
	return 0;
}

        如果线程需要绑定的函数有参数则需要使用boost::bind。比如想使用 boost::thread创建一个线程来执行函数:void f(int i),如果这样写:boost::thread thrd(f)是不对的,因为thread构造函数声明接受的是一个没有参数且返回类型为 void 的类别,而且不提供参数 i 的值 f 也无法运行,这时就可以写:boost::thread thrd(boost::bind(f,1))。涉及到有参函数的绑定问题基本上都是boost::thread、boost::function、boost::bind结合起来使用。 


方式二:用类内函数创建线程

#include <boost/thread/thread.hpp>
#include <boost/bind.hpp>
#include <iostream>
//Linux make: g++ -o main main8.c -lboost_system -lboost_thread

class HelloWorld{
public:
	void hello(){
		std::cout << "Hello world, I'm a thread!" << std::endl;
	}
	void start(){
		boost::thread thrd(boost::bind(&HelloWorld::hello,this));
		thrd.join();
	}
};

int main(int argc, char* argv[]){
	HelloWorld hello;
	hello.start();
	return 0;
}

方式三:用类内函数创建线程

#include <boost/thread/thread.hpp>
#include <iostream> 
//Linux make: g++ -o main main8.c -lboost_system -lboost_thread

class HelloWorld{
public:
	static void hello(){
		std::cout << "Hello world, I'm a thread!" << std::endl;
	}
	static void start(){
		boost::thread thrd(hello);
		thrd.join();
	}
};

int main(int argc, char* argv[]){
	HelloWorld::start();
	return 0;
}

方式四:类外用类内函数创建线程

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

class HelloWorld{
public:
	void hello(const std::string& str){
		std::cout << str << std::endl;
	}
};

int main(int argc, char* argv[]){
	HelloWorld obj;
	boost::thread thrd(boost::bind(&HelloWorld::hello, &obj, "Hello world, I'm a thread!"));
	thrd.join();
	return 0;
}

更多方式参考 

boost::bind的用法:

        得益于c++的模板以及操作符重载,去看boost::bind的实现就会发现它是一个有n多重载的函数,这些重载主要是为了适应函数的参数个数。

        其实boost::bind的原理是函数对象,而函数对象就是一个重载了()操作符的对象,这样我们就可以像调用一个方法一样来调用一个类上的这个操作符,比如a(),其实你是在调用a这个对象的()方法,而不是调用一个叫a的方法。

        一般来说boost::bind有两种方式的调用,一种是对自由方法,也取非类方法, 一种是对类方法。

  • 自由方法:boost::bind(函数名, 参数1,参数2,...)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值