1. 前言
在实际的开发中,我们一般比较习惯使用一些已经的程序模块,最常用的就是STL,这个库是已经被标准化了的库,里面的很多的容器都已经做了最优化的处理,效率方面那不用多说,但是在实际的开发中,我们通常会涉及到比STL更多的内容,例如各种序列化操作以及网络多线程等,这是我们一般都会去寻找一些第三方的库,在这其中,boost这个库就不可不说了,这个库很NB,里面包含了很多的子模块,涉及的范围超广,今天我们就来学习boost库中的thread模块吧,先来看几个简单点的实例吧,这些实例也是从网上看来的,代码如下:
#include <stdlib.h>
#include <stdio.h>
#include <string>
#include <boost/function.hpp>
#include <boost/bind.hpp>
#include <boost/thread/thread.hpp>
static void* worker()
{
printf("Hello World\n");
return NULL;
}
int main()
{
boost::thread thread1(&worker);
boost::thread thread2(&worker);
thread1.join();
thread2.join();
return 0;
}
代码很简单,直接通过指令g++ -o test test.cpp -lboost_thread即可,如果出现错误的话,需要到你安装目录下执行ldconfig这个指令即可,这种方式创建的线程很简单,通过将定义好的回调函数传给线程,而后线程将会自动执行该函数,接下来,我们就来看看几个比较复杂的例子,代码如下:
#include <string>
#include <stdio.h>
#include <boost/thread/thread.hpp>
class Object
{
public:
Object(const char* name):name(name){};
~Object(){};
void operator()()const
{
printf("thread name:%s\n",name.c_str());
}
private:
std::string name;
};
#include "Object.h"
int main()
{
boost::thread thread1(Object("worker1"));
boost::thread thread2(Object("worker2"));
thread1.join();
thread2.join();
return 0;
}
在这个案例中,我们提供给boost线程传入一个对象的形式来实现调用,其实在被传入对象的类里面我们重载了(),之所以需要重载这个运算符,一个最主要的原因就是在boost的thread中将我们传入的对象看做成函数对象,因此通过直接调用对象的方式实现了函数调用,接下来,我们再来看看另外的例子,代码如下:
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <boost/thread/thread.hpp>
#include <boost/function.hpp>
#include <boost/bind.hpp>
void worker()
{
printf("this is not argument\n");
}
void worker(int a,int b)
{
int sum = a+b;
printf("this is two arguments,sum=%d\n",sum);
}
void worker(std::string str)
{
printf("this is a string arguments,string=%s\n",str.c_str());
}
int main()
{
int a,b;
std::string str = "HelloWord";
a = 2,b=4;
boost::thread thread1(boost::bind(&worker));
boost::thread thread2(boost::bind(&worker,a,b));
boost::thread thread3(boost::bind(&worker,str));
thread1.join();
thread2.join();
thread3.join();
return 0;
}
在这段代码里面,我们使用了boost::bind这个函数,这个函数的作用就是用于将我们的回调函数指定给线程,使用方式很简单,希望大家能够好好地体会一下这种方法,另外还可以通过使用boost::function这个函数来为线程设定回调函数,将上面的代码稍作修改,代码如下:
int main()
{
int a,b;
std::string str = "HelloWord";
a = 2,b=4;
boost::function<void()> f1 = boost::bind(&worker);
boost::function<void()> f2 = boost::bind(&worker,a,b);
boost::function<void()> f3 = boost::bind(&worker,str);
boost::thread thread1(f1);
boost::thread thread2(f2);
boost::thread thread3(f3);
thread1.join();
thread2.join();
thread3.join();
return 0;
}
其实除了上面这些创建线程之外,我们一般是通过类的方式来构建线程,接下来,我们就使用类的方式来实现以下几种创建线程的方法,代码如下:
#include <stdio.h>
#include <string>
#include <stdlib.h>
#include <pthread.h>
#include <boost/function.hpp>
#include <boost/bind.hpp>
class Thread
{
typedef boost::function<void()> threadfun;
public:
Thread(const threadfun& fun,const char* name):fun(fun),name(name){}
~Thread(){};
void run();
private:
static void* start(void* obj)
{
Thread* thr = static_cast<Thread*>(obj);
thr->fun();
return NULL;
}
private:
threadfun fun;
std::string name;
};
#include "Thread.h"
void Thread::run()
{
pthread_t thr;
pthread_create(&thr,NULL,&start,this);
sleep(2);
pthread_join(thr,NULL);
}
void worker()
{
printf("this is not arguments\n");
}
void worker(int a ,int b)
{
int sum = a + b;
printf("this is two arguments,sum=%d\n",sum);
}
int main()
{
int a,b;
a = b = 3;
boost::function<void()> f = boost::bind(&worker);
boost::function<void()> f1 = boost::bind(&worker,a,b);
Thread thr1(f,"thread1");
Thread thr2(f1,"thread2");
thr1.run();
thr2.run();
}
在上述代码中,我们从给Thread对象传递了一个boost::function对象,并且最后由Thread对象来执行,这个过程其实很简单,在Thread中对pthread进行了封装,通过这种方式我们可以不断地扩展开来,从而使其能够适用于实际的开发场合,在boost中,创建线程其实只需要两个步骤即可,1)指定回调函数,2)使用指定的回调函数创建线程,3)对那自定义的Thread可能需要还需要自行执行,整体来讲,boost里面对thread这方面的支持还不错,在接下来的博文中,我们将会针对thread这部分的方方面面来探讨,这篇博文主要是我们开始boost的起点,所以写的有点简单,不过从下篇起,我们就要开始分析其中的源代码,并从中总结值得我们的学习的经验。总结
boost库说实话,真心不错,里面实现的一些库在我们实际开发中会经常用到,所以有必要对boost中的库进行分析总结,并且达到熟练掌握的程度,这样无疑能够提今后的开发效率,不用再为了某些功能而重复造轮子了,对boost的学习过程将会是一个长久地过程,通过分析其中的设计思路,来提高自己的编码能力,今天的博文到此就结束了,多谢了如果需要,请注明转载,多谢了