生活随笔
收集整理的這篇文章主要介紹了
linux下boost的一个扩展线程池-threadpool-的学习
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
轉(zhuǎn)自:http://www.cnblogs.com/xiaouisme/archive/2012/10/04/2711691.html
安裝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
callback_task.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()); }
};}}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
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;pool tp(
0);help2AddAllTask(tp);help2SeePoolStatus(tp);tp.wait();
}
void testCase1()
{
cout<<
"testCase1()\n" << endl;pool tp(
1);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);
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
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é)
以上是生活随笔為你收集整理的linux下boost的一个扩展线程池-threadpool-的学习的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。