Linux C下实现线程池
生活随笔
收集整理的這篇文章主要介紹了
Linux C下实现线程池
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
下面是原文鏈接,我是照著敲的。。。hi.baidu.com/boahegcrmdghots/item/f3ca1a3c2d47fcc52e8ec2e1
?
#include<stdio.h> #include<stdlib.h> #include<unistd.h> #include<sys/types.h> #include<pthread.h> #include<assert.h>/* 線程池里所有運行和等待的任務都是一個thread_job 由于所有任務都在鏈表中,所以是一個鏈表結構 */ typedef struct job {/*回調函數,任務運行時會調用次函數,注意也可以聲明為其他形式*/void * (*process)(void *arg);void *arg;struct job *next; }thread_job;/*線程池結構*/ typedef struct {pthread_mutex_t queue_lock;pthread_cond_t queue_ready;/*鏈表結構,線程池中所有等待任務*/thread_job *queue_head;/*線程池銷毀標記*/int destroy;/**/pthread_t *threadid;/*線程池中允許的最大線程數目*/int max_thread_num;/*等待隊列的任務數目*/int cur_queue_size; }thread_pool;int add_job(void *(*process)(void *arg), void *arg); void *thread_runtine(void *arg);/*把線程池指針設為靜態*/ static thread_pool *pool = NULL;/*初始化線程池*/ void pool_init(int max_thread_num) {pool = (thread_pool *)malloc(sizeof(thread_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->threadid = (pthread_t *)malloc(max_thread_num*sizeof(pthread_t));int i = 0;/*創建max_thread_num個線程*/for(i=0; i<max_thread_num; i++){/*理解線程創建函數各個參數, 每個線程都會執行thread_runtine函數*/pthread_create(&(pool->threadid[i]), NULL, thread_runtine, NULL);} } /*每個線程都會執行runtine函數, 每個thread_rutine函數都是取出一個thread_job處理任務 也就是說,線程池中可能有很多個thread_job需要cur_queue_size個線程去處理,處理前要 先對線程池加鎖,保證當前處理任務的線程處理thread_job時不會收到其他線程影響,能夠互斥訪問*/ void *thread_runtine(void *arg) {printf("starting thread 0x%x\n", (unsigned int)pthread_self());while(1){/*對線程池加互斥鎖*/pthread_mutex_lock(&(pool->queue_lock));/*如果線程當前的等待隊列為0,并且沒有被銷毀,則處于阻塞狀態pthread_cond_wait是一個原子操作,等待前解鎖,喚醒后會加鎖*/while( (pool->cur_queue_size == 0) && (!pool->destroy) ){printf("thread 0x%x is waiting\n", (unsigned int)pthread_self());pthread_cond_wait(&(pool->queue_ready), &(pool->queue_lock));}/*線程池銷毀了*/if(pool->destroy){pthread_mutex_lock(&(pool->queue_lock));printf("thread 0x%x will exit\n", (unsigned int)pthread_self());pthread_exit(NULL);}printf("thread 0x%x is starting to work\n", (unsigned int)pthread_self());assert(pool->cur_queue_size != 0);assert(pool->queue_head != NULL);/*等待隊列長度減1,并取出線程池中的線程鏈表中的頭元素*/pool->cur_queue_size--;thread_job *job = pool->queue_head;pool->queue_head = job->next;pthread_mutex_unlock(&(pool->queue_lock));/*調用回調函數,執行任務, */(*(job->process))(job->arg);free(job);job = NULL;}/*這一句是不可達的*/pthread_exit(NULL); } /*向線程池中添加任務, 重點理解參數, 每個任務要做的事體現在process回調函數上*/ int pool_add_job(void *(*process)(void *arg), void *arg) {thread_job *newjob = (thread_job *)malloc(sizeof(thread_job));newjob->process = process;newjob->arg = arg;newjob->next = NULL;/*對線程池加鎖*/pthread_mutex_lock(&(pool->queue_lock));thread_job *member = pool->queue_head;if(member != NULL){while(member->next != NULL)member = member->next;member->next = newjob;}else{pool->queue_head = newjob;}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->destroy)return -1;pool->destroy = 1;/*喚醒所有等待線程,線程池要銷毀了*/pthread_cond_broadcast(&(pool->queue_ready));/*阻塞等待線程退出,否則變成僵尸了*/int i = 0;for(i=0; i<pool->max_thread_num; i++){pthread_join(pool->threadid[i], NULL);}free(pool->threadid);thread_job *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* myprocess(void *arg) {printf("threadid is 0x%x, working on task %d\n", (unsigned int)pthread_self(), *(int *)arg);sleep(1);return NULL; } int main(int argc, char **argv) {pool_init(3);/*初始話含有3個線程的線程池*/int *workingnum = (int *)malloc(sizeof(int)*10);int i = 0;for(i=0; i<10; i++){workingnum[i] = i;pool_add_job(myprocess, &workingnum[i]);}sleep(5);pool_destroy();free(workingnum);return 0; }?
?
總結
以上是生活随笔為你收集整理的Linux C下实现线程池的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ssh证书登录(实例详解)
- 下一篇: Exchange Server2010系