一、什麼是線程池
應用程序可以有多個線程,這些線程在休眠狀態中需要耗費大量時間來等待事件發生。其他線程可能進入睡眠狀態,並且僅定期被喚醒以輪循更改或更新狀態信息,然後再次進入休眠狀態。為了簡化對這些線程的管理,.NET框架為每個進程提供了一個線程池,一個線程池有若干個等待操作狀態,當一個等待操作完成時,線程池中的輔助線程會執行回調函數。線程池中的線程由系統管理,程序員不需要費力於線程管理,可以集中精力處理應用程序任務。
線程池是一種多線程處理形式,處理過程中將任務添加到隊列,然後在創建線程後自動啟動這些任務。線程池線程都是後台線程.每個線程都使用默認的堆棧大小,以默認的優先級運行,並處於多線程單元中.如果某個線程在托管代碼中空閒(如正在等待某個事件),則線程池將插入另一個輔助線程來使所有處理器保持繁忙.如果所有線程池線程都始終保持繁忙,但隊列中包含掛起的工作,則線程池將在一段時間後創建另一個輔助線程但線程的數目永遠不會超過最大值.超過最大值的線程可以排隊,但他們要等到其他線程完成後才啟動
二、什麼情況下不要使用線程池
●如果需要使一個任務具有特定優先級
●如果具有可能會長時間運行(並因此阻塞其他任務)的任務
●如果需要將線程放置到單線程單元中(線程池中的線程均處於多線程單元中)
●如果需要永久標識來標識和控制線程,比如想使用專用線程來終止該線程,將其掛起或按名稱發現它
下面給出一個線程池的例子,以供大家參考:
[cpp]
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <pthread.h>
- #include <assert.h>
-
- /*
- *線程池裡所有運行和等待的任務都是一個CThread_worker
- *由於所有任務都在鏈表裡,所以是一個鏈表結構
- */
- typedef struct worker
- {
- /*回調函數,任務運行時會調用此函數,注意也可聲明成其它形式*/
- void *(*process) (void *arg);
- void *arg;/*回調函數的參數*/
- struct worker *next;
-
- } CThread_worker;
-
-
-
- /*線程池結構*/
- typedef struct
- {
- pthread_mutex_t queue_lock;
- pthread_cond_t queue_ready;
-
- /*鏈表結構,線程池中所有等待任務*/
- CThread_worker *queue_head;
-
- /*是否銷毀線程池*/
- int shutdown;
- pthread_t *threadid;
- /*線程池中允許的活動線程數目*/
- int max_thread_num;
- /*當前等待隊列的任務數目*/
- int cur_queue_size;
-
- } CThread_pool;
-
-
-
- int pool_add_worker (void *(*process) (void *arg), void *arg);
- void *thread_routine (void *arg);
-
-
-
- static CThread_pool *pool = NULL;
- void pool_init (int max_thread_num)
- {
- pool = (CThread_pool *) malloc (sizeof (CThread_pool));
-
- pthread_mutex_init (&(pool->queue_lock), NULL);
- pthread_cond_init (&(pool->queue_ready), NULL);
-
- pool->queue_head = NULL;
-
- pool->max_thread_num = max_thread_num;
- pool->cur_queue_size = 0;
-
- pool->shutdown = 0;
-
- pool->threadid =
- (pthread_t *) malloc (max_thread_num * sizeof (pthread_t));
- int i = 0;
- for (i = 0; i < max_thread_num; i++)
- {
- pthread_create (&(pool->threadid[i]), NULL, thread_routine,
- NULL);
- }
- }
-
-
-
- /*向線程池中加入任務*/
- int
- pool_add_worker (void *(*process) (void *arg), void *arg)
- {
- /*構造一個新任務*/
- CThread_worker *newworker =
- (CThread_worker *) malloc (sizeof (CThread_worker));
- newworker->process = process;
- newworker->arg = arg;
- newworker->next = NULL;/*別忘置空*/
-
- pthread_mutex_lock (&(pool->queue_lock));
- /*將任務加入到等待隊列中*/
- CThread_worker *member = pool->queue_head;
- if (member != NULL)
- {
- while (member->next != NULL)
- member = member->next;
- member->next = newworker;
- }
- else
- {
- pool->queue_head = newworker;
- }
-
- assert (pool->queue_head != NULL);
-
- pool->cur_queue_size++;
- pthread_mutex_unlock (&(pool->queue_lock));
- /*好了,等待隊列中有任務了,喚醒一個等待線程;
- 注意如果所有線程都在忙碌,這句沒有任何作用*/
- pthread_cond_signal (&(pool->queue_ready));
- return 0;
- }
-
-
-
- /*銷毀線程池,等待隊列中的任務不會再被執行,但是正在運行的線程會一直
- 把任務運行完後再退出*/
- int pool_destroy ()
- {
- if (pool->shutdown)
- return -1;/*防止兩次調用*/
- pool->shutdown = 1;
-
- /*喚醒所有等待線程,線程池要銷毀了*/
- pthread_cond_broadcast (&(pool->queue_ready));
-
- /*阻塞等待線程退出,否則就成僵屍了*/
- int i;
- for (i = 0; i < pool->max_thread_num; i++)
- pthread_join (pool->threadid[i], NULL);
- free (pool->threadid);
-
- /*銷毀等待隊列*/
- CThread_worker *head = NULL;
- while (pool->queue_head != NULL)
- {
- head = pool->queue_head;
- pool->queue_head = pool->queue_head->next;
- free (head);
- }
- /*條件變量和互斥量也別忘了銷毀*/
- pthread_mutex_destroy(&(pool->queue_lock));
- pthread_cond_destroy(&(pool->queue_ready));
-
- free (pool);
- /*銷毀後指針置空是個好習慣*/
- pool=NULL;
- return 0;
- }
-
-
-
- void * thread_routine (void *arg)
- {
- printf ("starting thread 0x%x\n", pthread_self ());
- while (1)
- {
- pthread_mutex_lock (&(pool->queue_lock));
- /*如果等待隊列為0並且不銷毀線程池,則處於阻塞狀態; 注意
- pthread_cond_wait是一個原子操作,等待前會解鎖,喚醒後會加鎖*/
- while (pool->cur_queue_size == 0 && !pool->shutdown)
- {
- printf ("thread 0x%x is waiting\n", pthread_self ());
- pthread_cond_wait (&(pool->queue_ready), &(pool->queue_lock));
- }
-
- /*線程池要銷毀了*/
- if (pool->shutdown)
- {
- /*遇到break,continue,return等跳轉語句,千萬不要忘記先解鎖*/
- pthread_mutex_unlock (&(pool->queue_lock));
- printf ("thread 0x%x will exit\n", pthread_self ());
- pthread_exit (NULL);
- }
-
- printf ("thread 0x%x is starting to work\n", pthread_self ());
-
- /*assert是調試的好幫手*/
- assert (pool->cur_queue_size != 0);
- assert (pool->queue_head != NULL);
-
- /*等待隊列長度減去1,並取出鏈表中的頭元素*/
- pool->cur_queue_size--;
- CThread_worker *worker = pool->queue_head;
- pool->queue_head = worker->next;
- pthread_mutex_unlock (&(pool->queue_lock));
-
- /*調用回調函數,執行任務*/
- (*(worker->process)) (worker->arg);
- free (worker);
- worker = NULL;
- }
- /*這一句應該是不可達的*/
- pthread_exit (NULL);
- }
-
- void * myprocess (void *arg)
- {
- printf ("threadid is 0x%x, working on task %d\n", pthread_self (),*(int *) arg);
- sleep (1);/*休息一秒,延長任務的執行時間*/
- return NULL;
- }
-
- int main (int argc, char **argv)
- {
- pool_init (3);/*線程池中最多三個活動線程*/
-
- /*連續向池中投入10個任務*/
- int *workingnum = (int *) malloc (sizeof (int) * 10);
- int i;
- for (i = 0; i < 10; i++)
- {
- workingnum[i] = i;
- pool_add_worker (myprocess, &workingnum[i]);
- }
- /*等待所有任務完成*/
- sleep (5);
- /*銷毀線程池*/
- pool_destroy ();
-
- free (workingnum);
- return 0;
- }