最近在写一个c++的linux app,要用到线程池,想着很多线程的很多的代码,就像借用一下。我选择了基于boost的boost threadpool。
安装boost:我选择的是boost1.51版本。
1.下载boost源码,http://sourceforge.net/projects/boost/files/boost/1.51.0/
2.我下载的zip,下载完毕,解压zip,获得boost源码。
3.源码的根目录:
./bootstrap.sh (默认安装到/usr/local/include和/usr/local/lib)
sudo ./b2 install
4.如果安装后,你看到有fail的情况,请安装二个软件包:
"安装过程中提示: patchlevel.h:没有那个文件或目录",解决:sudo apt-get install python-dev
"bzlib.h:没有那个文件或目录",解决:sudo apt-get install libbz2-dev
5.下载boost threadpool:
http://prdownloads.sourceforge.net/threadpool/threadpool-0_2_5-src.zip?download
6.threadpool解压后,搞个源码测试一下:
源码的内容为:
- #include <iostream>
- #include "threadpool.hpp"
- using namespace std;
- using boost::threadpool::pool;
- // Some example tasks
- void first_task()
- {
- cout << "first task is running\n" ;
- }
- void second_task()
- {
- cout << "second task is running\n" ;
- }
- void task_with_parameter(int value)
- {
- cout << "task_with_parameter(" << value << ")\n";
- }
- int main(int argc,char *argv[])
- {
- // Create fifo thread pool container with two threads.
- pool tp(2);
- // Add some tasks to the pool.
- tp.schedule(&first_task);
- tp.schedule(&second_task);
- tp.schedule(boost::bind(task_with_parameter, 4));
- // Wait until all tasks are finished.
- tp.wait();
- // Now all tasks are finished!
- return(0);
- }
7.将threadpool-0_2_5-src/threadpool/boost下的一个文件夹和头文件拷贝到test.cc目录。
8.然后就可以编译:
g++ test.cc -lboost_thread
发现TIME_UTC没有定义。这个问题来源于,boot1.51这个版本,将"TIME_UTC"改为了"TIME_UTC_",一个脑惨的改动。去threadpool里面报错的文件,把这个宏定义改成后者就好了。
9.编译通过,生成a.out可执行文件。
10.运行a.out,会报错,error while loading shared libraries: libboost_thread.so.1.51.0: cannot open shared object file: No such file or directory
11.加上一句:
sudo ldconfig /usr/local/lib
12.再执行程序,OK。