linux下boost的一個擴充線程池-threadpool-的學習

來源:互聯網
上載者:User

安裝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 <boost/threadpool.hpp>。(詳見文檔: "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 <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 poolhelp2AddAllTask(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)

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.