日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 运维知识 > linux >内容正文

linux

Linux C下实现线程池

發(fā)布時(shí)間:2025/4/16 linux 16 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Linux C下实现线程池 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

下面是原文鏈接,我是照著敲的。。。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>/* 線程池里所有運(yùn)行和等待的任務(wù)都是一個(gè)thread_job 由于所有任務(wù)都在鏈表中,所以是一個(gè)鏈表結(jié)構(gòu) */ typedef struct job {/*回調(diào)函數(shù),任務(wù)運(yùn)行時(shí)會(huì)調(diào)用次函數(shù),注意也可以聲明為其他形式*/void * (*process)(void *arg);void *arg;struct job *next; }thread_job;/*線程池結(jié)構(gòu)*/ typedef struct {pthread_mutex_t queue_lock;pthread_cond_t queue_ready;/*鏈表結(jié)構(gòu),線程池中所有等待任務(wù)*/thread_job *queue_head;/*線程池銷(xiāo)毀標(biāo)記*/int destroy;/**/pthread_t *threadid;/*線程池中允許的最大線程數(shù)目*/int max_thread_num;/*等待隊(duì)列的任務(wù)數(shù)目*/int cur_queue_size; }thread_pool;int add_job(void *(*process)(void *arg), void *arg); void *thread_runtine(void *arg);/*把線程池指針設(shè)為靜態(tài)*/ 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;/*為線程池中的各個(gè)線程分配空間 做初始化*/pool->threadid = (pthread_t *)malloc(max_thread_num*sizeof(pthread_t));int i = 0;/*創(chuàng)建max_thread_num個(gè)線程*/for(i=0; i<max_thread_num; i++){/*理解線程創(chuàng)建函數(shù)各個(gè)參數(shù), 每個(gè)線程都會(huì)執(zhí)行thread_runtine函數(shù)*/pthread_create(&(pool->threadid[i]), NULL, thread_runtine, NULL);} } /*每個(gè)線程都會(huì)執(zhí)行runtine函數(shù), 每個(gè)thread_rutine函數(shù)都是取出一個(gè)thread_job處理任務(wù) 也就是說(shuō),線程池中可能有很多個(gè)thread_job需要cur_queue_size個(gè)線程去處理,處理前要 先對(duì)線程池加鎖,保證當(dāng)前處理任務(wù)的線程處理thread_job時(shí)不會(huì)收到其他線程影響,能夠互斥訪問(wèn)*/ void *thread_runtine(void *arg) {printf("starting thread 0x%x\n", (unsigned int)pthread_self());while(1){/*對(duì)線程池加互斥鎖*/pthread_mutex_lock(&(pool->queue_lock));/*如果線程當(dāng)前的等待隊(duì)列為0,并且沒(méi)有被銷(xiāo)毀,則處于阻塞狀態(tài)pthread_cond_wait是一個(gè)原子操作,等待前解鎖,喚醒后會(huì)加鎖*/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));}/*線程池銷(xiāo)毀了*/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);/*等待隊(duì)列長(zhǎng)度減1,并取出線程池中的線程鏈表中的頭元素*/pool->cur_queue_size--;thread_job *job = pool->queue_head;pool->queue_head = job->next;pthread_mutex_unlock(&(pool->queue_lock));/*調(diào)用回調(diào)函數(shù),執(zhí)行任務(wù), */(*(job->process))(job->arg);free(job);job = NULL;}/*這一句是不可達(dá)的*/pthread_exit(NULL); } /*向線程池中添加任務(wù), 重點(diǎn)理解參數(shù), 每個(gè)任務(wù)要做的事體現(xiàn)在process回調(diào)函數(shù)上*/ 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;/*對(duì)線程池加鎖*/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));/*等待隊(duì)列中有任務(wù)了,喚醒一個(gè)等待線程,如果所有線程都在忙碌,這句沒(méi)有任何作用*/pthread_cond_signal(&(pool->queue_ready));return 0; }int pool_destroy() {if(pool->destroy)return -1;pool->destroy = 1;/*喚醒所有等待線程,線程池要銷(xiāo)毀了*/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);}/*條件變量和互斥量也要銷(xiāo)毀*/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個(gè)線程的線程池*/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; }


?

?

總結(jié)

以上是生活随笔為你收集整理的Linux C下实现线程池的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。