02基於linux的執行緒池設計

2021-10-02 12:11:00 字數 4650 閱讀 7680

/*

* @author: power

* @date: 2020-02-02 13:43:24

* @lastedittime : 2020-02-02 15:48:31

* @lasteditors : please set lasteditors

* @description: in user settings edit

* @filepath: /test/threadpool.h

*/#ifndef __threadpool_h_

/*define*/

#define __threadpool_h_

/*define*/

/* code */

#include

#include

#include

#include

#include

#include

#define max_waiting_tasks 1000

//最大任務數

#define max_active_threads 20

//最大執行緒數

/** * 任務節點結構體

**/struct task;/*

*執行緒池構建

*/struct threadpool

;/**

* 初始化執行緒池

* pool 執行緒池

* threads_number 執行緒池中維護執行緒的個數

**/bool

init_pool

(threadpool *pool,

unsigned

int threads_number)

;/**

* 執行緒排程函式

**/void

*dispath

(void

*arg)

;/**

* 任務新增函式

* pool 執行緒池

* do_task 任務函式指標

**/bool

add_task

(threadpool *pool,

void*(

*do_task)

(void

*arg)

,void

*arg)

;/**

* 銷毀執行緒池

* pool 執行緒池

**/bool

destory_pool

(threadpool *pool)

;#endif

///*define*/

/*

* @author: power

* @date: 2020-02-02 14:18:20

* @lastedittime : 2020-02-02 16:25:23

* @lasteditors : please set lasteditors

* @description: in user settings edit

* @filepath: /test/threadpool.cpp

*/#include

"threadpool.h"

using

namespace std;

void

handler

(void

*arg)

void

*dispath

(void

*arg)

if(pool-

>waiting_tasks ==

0&& pool-

>shutdown)

//做任務

p = pool-

>task_list-

>next;

pool-

>task_list-

>next = p-

>next;

pool-

>waiting_tasks--

;//任務數減一

pthread_mutex_unlock

(&pool-

>lock)

;pthread_cleanup_pop(0

);pthread_setcancelstate

(pthread_cancel_disable,

null);

//設定不可響應取消執行緒

p->

do_task

(p->arg)

;pthread_setcancelstate

(pthread_cancel_enable,

null);

free

(p);

}pthread_exit

(null);

}bool

init_pool

(threadpool *pool,

unsigned

int threads_number)

pool-

>task_list-

>next =

null

;//頭結點為空

//設定最大任務個數

pool-

>max_waiting_tasks = max_waiting_tasks;

//設定當前等待任務個數

pool-

>waiting_tasks =0;

//設定當前執行緒池執行緒個數

pool-

>active_threads = threads_number;

//建立執行緒

for(

int i =

0; i < pool-

>active_threads; i++)}

return

true;}

bool

add_task

(threadpool *pool,

void*(

*do_task)

(void

*arg)

,void

*arg)

//新節點初始化

new_task-

>do_task = do_task;

//任務函式

new_task-

>arg = arg;

//函式引數

new_task-

>next =

null

;//訪問任務佇列

pthread_mutex_lock

(&pool-

>lock);if

(pool-

>waiting_tasks>=max_waiting_tasks)

//新增任務到任務佇列中

task *tmp = pool-

>task_list;

while

(tmp-

>next!=

null

) tmp-

>next = new_task;

pool-

>waiting_tasks++

;//解鎖任務結束

pthread_mutex_unlock

(&pool-

>lock)

;pthread_cond_signal

(&pool-

>cond)

;//喚醒執行緒

return

true;}

bool

destory_pool

(threadpool *pool)

else

}free

(pool-

>task_list)

;free

(pool-

>tids)

;free

(pool)

;return

true

;}

/*

* @author: power

* @date: 2020-02-02 15:45:08

* @lastedittime : 2020-02-02 16:28:43

* @lasteditors : please set lasteditors

* @description: in user settings edit

* @filepath: /test/testpool.cpp

*/#include

"threadpool.cpp"

using

namespace std;

void

*mytask

(void

*arg)

intmain

(int argc,

char

const

*ar**)

13436904961335297792job will be done injob will be done in45sec…sec…

1343690496job done

1343690496job will be done in6sec…

1335297792job done

1335297792任務結束.

1343690496job done

1343690496任務結束.

1343690496執行緒已被關閉

1335297792執行緒已被關閉

執行緒池 02 Executor框架

executor 二 executor 的組成 2 任務的執行 3 非同步計算的結果 三 executor 使用 四 executor 主要的類與介面的簡介 2 threadpoolexecutor 3 scheduledthreadpoolexecutor 定時任務類,這裡不討論 4 future...

Linux執行緒池

linux下通用執行緒池的建立與使用 本文給出了乙個通用的執行緒池框架,該框架將與執行緒執行相關的任務進行了高層次的抽象,使之與具體的執行任務無關。另外該執行緒池具有動態伸縮性,它能根據執行任務的輕重自動調整執行緒池中線程的數量。文章的最後,我們給出乙個簡單示例程式,通過該示例程式,我們會發現,通過...

Linux 執行緒池

一.功能 執行緒池中有若干個執行緒,用於執行大量相對短暫的任務 二.功能描述 計算密集型任務 執行緒池中線程個數 cpu個數 i o密集型任務 執行緒池中線程個數 cpu個數 當任務增加時,可以動態增加執行緒池中線程個數,當任務執行完成後,可以動態減少執行緒池中線程個數 運用生產消費者模型,生產者執...