转自:http://www.cnblogs.com/xiaouisme/archive/2012/10/04/2711691.html
安装boost:
http://www.boost.org/下载boost,我下下来是boost_1_51_0.
boost库的大部分都可以直接引用头文件就行了,因为大多数都是头文件里模板加inline函数构成。但是也有些是需要安装成二进制lib的,比如thread.(详见文档:”Getting Started…”)
$ cd boost_1_51_0
$ sudo ./bootstrap.sh //这条命令类似./configure. 也可以./bootstrap.sh --help看看有哪些命令参数.
$ sudo ./b2 install //这样,boost库的所有头文件和需要编译的lib都安装到/usr/local/lib 和 /usr/local/include了。(头文件在boost文件夹里.)
boost扩展工具-线程池(threadpool):
http://threadpool.sourceforge.net/下载threadpool,然后把threadpool里面的boost目录下的threadpool.hpp和threadpool文件夹拷贝到/usr/local/include/boost/下(如果有权限问题还得cd /usr/local/include/boost && sudo chmod -R 777 *).
使用threadpool需要链接boost的两个共享库:boost_thread、boost_system(如果是静态链接那就还得动态链接pthread库), 并且include
callback_task.hpp:
/*
* @file callback_task.hpp
* @brief add callback task for threadpool.
*/
#ifndef __callback_task_h__
#define __callback_task_h__
#include <boost/function.hpp>
namespace boost { namespace threadpool
{
template<class RetType>
class callback_task
{
typedef boost::function<void (RetType)> CALLBACK;
typedef boost::function<RetType ()> FUNCTION;
private:
CALLBACK m_Callback;
FUNCTION m_Function;
public:
callback_task(FUNCTION f, CALLBACK c):m_Callback(c), m_Function(f){}
void operator()(){ m_Callback(m_Function()); }
};
}} // namespace boost::threadpoll
#endif // __callback_task_h__
main.cpp:
#include <iostream>
#include <sstream>
#include <boost/threadpool.hpp>
#include "callback_task.hpp"
using namespace std;
using namespace boost::threadpool;
void task_normal()
{
cout << "task_normal()\n";
}
void task_with_parameter(int value, string str)
{
cout << "task_with_parameter(" << value << ", " << str << ")\n";
}
bool task_loop()
{
static int i = 0;
cout << "task_loop:" << i <<"\n";
return ++i != 5;
}
int task_return14()
{
sleep(1);
return 14;
}
void callback(int ret)
{
cout<< "callback: task_return14() return " << ret << "\n";
}
void task_test4ThreadPrivateData()
{
cout << "task_test4ThreadPrivateData().id:";
static map<boost::thread::id, string> s_ThreadPrivateData;
boost::thread::id tid = boost::this_thread::get_id();
cout << tid << "\n";
map<boost::thread::id, string>::iterator it;
if((it = s_ThreadPrivateData.find(tid)) == s_ThreadPrivateData.end())
{
it = s_ThreadPrivateData.insert(make_pair(tid, "hello")).first;
}
cout << tid << " has private data:" << it->second << "\n";
}
void help2SeePoolStatus(pool & tp)
{
ostringstream os;
os << "begin>\n";
os << "how many threads in the pool:" << tp.size() << "\n";
os << "how many tasks are currently executed:" << tp.active() << "\n";
os << "how many tasks are ready and waiting for execution:" << tp.pending() << "\n";
os << "<end.";
cout<< "\033[1;45;33m"<< os.str() << "\033[0m" << "\n";
}
void help2AddAllTask(pool & tp)
{
tp.schedule( callback_task<int>(&task_return14, callback) );
tp.schedule(&task_normal);
tp.schedule(boost::bind(task_with_parameter, 4, "number"));
tp.schedule( looped_task_func(&task_loop, 0.5*1000) );
tp.schedule(&task_test4ThreadPrivateData);
}
void testCase0()
{
cout<< "testCase0()\n" << endl;
// Create fifo thread pool container with n threads.
pool tp(0);// 0 threads in pool
help2AddAllTask(tp);
help2SeePoolStatus(tp);
//Wait until all task are finished.
tp.wait();
}
void testCase1()
{
cout<< "testCase1()\n" << endl;
pool tp(1);// only one thread in pool.
help2AddAllTask(tp);
help2SeePoolStatus(tp);
tp.size_controller().resize(5);
help2SeePoolStatus(tp);
tp.wait();
help2SeePoolStatus(tp);
}
void testCase2()
{
cout<< "testCase2()\n" << endl;
pool tp(10);
help2AddAllTask(tp);
for(int i = 0; i != 4; i++, help2SeePoolStatus(tp), sleep(.5));
tp.wait();
}
int main(int argc,char *argv[])
{
testCase1();
return(0);
}
CMakeLists.txt:
cmake_minimum_required(VERSION 2.8)
project(test)
SET(CMAKE_C_COMPILER "g++")
SET(SRC_LIST main.cpp)
ADD_EXECUTABLE(${PROJECT_NAME} ${SRC_LIST})
TARGET_LINK_LIBRARIES(${PROJECT_NAME} boost_thread boost_system)