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即可,如果出现错误的话,需要到你安装目录下