muduo库的介绍:一个基于reactor反应堆模型的多线程C++网络库。作者是陈硕大神。
muduo库是基于 boost 库开发的,所以需要在 Linux 平台上首先安装 boost 库。
1、boost 库安装
1、编译源码安装
第一种方法是编译源码安装,可以了解一下,不过比较麻烦:
- 1、准备好boost压缩包,我这里是boost_1_65_0.tar.gz
- 2、解压 tar -zxvf boost_1_65_0.tar.gz /usr/local
- 3、进入/usr/local/boost_1_65_0目录,执行./bootstrap.sh
- 4、执行./bjam
- 5、执行./b2
- 6、cp -rf boost /usr/include
- 7、cp -rf stage/lib/* /usr/lib
因为g++会自动从/usr/include和/usr/local/lib路径下寻找所需要的文件。所以把这两部分复制到其中,当然也可以用软连接或者其他方式,使得程序在运行时候找到动态库。
2、操作系统仓库安装
在现在比较新的 Centos 或者 Ubuntu中,可以直接用操作系统仓库中的去安装,以Centos为例
- yum install boost
- yum install boost-devel
- yum install boost-doc
这样就安装好了,然后可以进行测试,以下面代码为例,编译命令为
-
g++ b.cpp -g -o b -lboost_thread
#include <boost/thread/thread.hpp>
#include <iostream>
#include <cstdlib>
using namespace std;
volatile bool isRuning = true;
void func1(){
static int cnt1 = 0;
while(isRuning){
cout << "func1:" << cnt1++ << endl;
sleep(1);
}
}
void func2(){
static int cnt2 = 0;
while(isRuning){
cout << "\tfunc2:" << cnt2++ << endl;
sleep(2);
}
}
int main()
{
boost::thread thread1(&func1);
boost::thread thread2(&func2)