boost创建线程池_linux下boost的一个扩展线程池-threadpool-的学习
安裝boost:
http://www.boost.org/下載boost,我下下來是boost_1_51_0.
boost庫的大部分都可以直接引用頭文件就行了,因為大多數(shù)都是頭文件里模板加inline函數(shù)構(gòu)成。但是也有些是需要安裝成二進制lib的,比如thread.(詳見文檔:"Getting Started...")
$ cd boost_1_51_0
$ sudo ./bootstrap.sh //這條命令類似./configure. 也可以./bootstrap.sh --help看看有哪些命令參數(shù).
$ 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/下(如果有權(quán)限問題還得cd /usr/local/include/boost && sudo chmod -R 777 *).
使用threadpool需要鏈接boost的兩個共享庫:boost_thread、boost_system(如果是靜態(tài)鏈接那就還得動態(tài)鏈接pthread庫), 并且include 。(詳見文檔: "Installing & Using threadpool")
在使用threadpool時,編譯會報錯:
task_adaptors.hpp:149:28: error: ‘TIME_UTC’ was not declared in this scope。。。
這是因為boost::TIME_UTC has been renamed to boost::TIME_UTC_ in Boost 1.50。修改task_adaptors.hpp自不必說.
http://blog.csdn.net/byxdaz/article/details/6299020
代碼:
callback_task.hpp:
/*
* @file callback_task.hpp
* @brief add callback task for threadpool.
*/
#ifndef __callback_task_h__
#define __callback_task_h__
#include
namespace boost { namespace threadpool
{
template
class callback_task
{
typedef boost::function CALLBACK;
typedef boost::function 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
#include
#include
#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 <
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<:thread::id string> s_ThreadPrivateData;
boost::thread::id tid = boost::this_thread::get_id();
cout << tid << "\n";
map<: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 << "
cout<< "\033[1;45;33m"<< os.str() << "\033[0m" << "\n";
}
void help2AddAllTask(pool & tp)
{
tp.schedule( callback_task(&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)
總結(jié)
以上是生活随笔為你收集整理的boost创建线程池_linux下boost的一个扩展线程池-threadpool-的学习的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 从根本上降低采购成本,不割韭菜~
- 下一篇: Ubuntu 命令行 安装 Operat