資源簡介
用c++ 實現的線程池工程,可直接使用,其中線程數目可根據空閑情況自增減,并且實現線程跟任務無關聯。

代碼片段和文件信息
#include?
#include?
#include?“threadpoolmanager.h“
#include?
#include?
#include?
#include?
?
?
using?namespace?std;
int?seq?=?0;
int?billNum?=0;
int?inter?=?1;
pthread_mutex_t?m_mutex;
void?myFunc(void*arg)
{
????pthread_mutex_lock(&m_mutex);
????seq++;
????if(seq%inter?==?0?)
????{
????????cout?<“fun?1=“?<????}
????if(seq>=1000000000)
????{
????????cout?<“billion“?<????????seq?=?0;
????????billNum++;
????}
????pthread_mutex_unlock(&m_mutex);
????//sleep();
}
?
int?main(int?argc?char**?argv)
{
????if(argc?!=?6)
????{
????????cout?<“必須有5個參數?任務執行次數?任務池大小?線程池大小?預創建線程數?輸出間隔“?<????????cout?<“eg:?./test?999999?10000?100?10?20“?<????????cout?<“上例代表創建一個間隔20個任務輸出,任務池大小為10000,線程池大小為100,預創建10個線程,執行任務次數為:999999“?<????????return?0;
????}
????double?loopSize?=?atof(argv[1]);
????int?taskSize?=?atoi(argv[2]);
????int?threadPoolSize?=?atoi(argv[3]);
????int?preSize?=?atoi(argv[4]);
????inter?=?atoi(argv[5]);
?
????pthread_mutex_init(&m_mutexNULL);
????ThreadPoolManager?manager;
????if(0>manager.Init(taskSize??threadPoolSize?preSize))
????{
????????cout?<“初始化失敗“?<????????return?0;
????}
????cout?<“*******************初始化完成*********************“?<????struct?timeval?time_beg?time_end;
????memset(&time_beg?0?sizeof(struct?timeval));
????memset(&time_end?0?sizeof(struct?timeval));?
????gettimeofday(&time_beg?NULL);
????double?i=0;
????for(;?i ????{
????????while(0>manager.Run(myFuncNULL))
????????{
????????????usleep(100);
????????}
????}
????gettimeofday(&time_end?NULL);
????long?total?=?(time_end.tv_sec?-?time_beg.tv_sec)*1000000?+?(time_end.tv_usec?-?time_beg.tv_usec);
????cout?<“total?time?=“?<????cout?<“total?num?=“?<????cout?<????//pthread_mutex_destroy(&m_mutex);
????return?0;
}
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
????.......???????440??2018-12-20?14:48??threadpool\commondef.h
????.......??????2143??2018-12-20?14:51??threadpool\main.cpp
????.......???????468??2018-12-20?16:26??threadpool\Makefile
????.......??????4670??2018-12-20?14:48??threadpool\taskpool.cpp
????.......??????2569??2018-12-20?14:49??threadpool\taskpool.h
????.......??????7999??2018-12-20?14:53??threadpool\threadpool.cpp
????.......??????3085??2018-12-20?14:50??threadpool\threadpool.h
????.......??????4111??2018-12-20?14:50??threadpool\threadpoolmanager.cpp
????.......??????3528??2018-12-20?14:51??threadpool\threadpoolmanager.h
?????目錄??????????0??2018-12-20?16:59??threadpool
-----------?---------??----------?-----??----
????????????????29013????????????????????10
評論
共有 條評論