Linux下调用pthread库实现简单线程池

[c-sharp]  view plain copy
  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include <unistd.h>  
  4. #include <sys/types.h>  
  5. #include <pthread.h>  
  6. #include <assert.h>  
  7. void * routine(void * arg);  
  8. int pool_add_job(void *(*process)(void * arg),void *arg);  
  9. int pool_init(unsigned int thread_num);  
  10. int pool_destroy(void);  
  11. void *test(void *);  
  12. /* We define a queue of jobs which will be processed in thread pool*/  
  13. typedef struct job{  
  14.     void * (*process)(void *arg); /* process() will employed on job*/  
  15.     void *arg;/* argument to process */  
  16.     struct job * next;  
  17. }Job;  
  18. /*  
  19.  * A threadpool must have the following parts: 
  20.  * threads:         a list of threads 
  21.  * jobs   :         jobs in thread pool 
  22.  * pool_lock:       pthread_mutex_lock for thread pool 
  23.  * job_ready:       pthread_cond_t for job ready 
  24.  * destroy:         flag indicate whether the pool shall be destroyed 
  25.  * size:            current size of jobs list 
  26.  * thread_num:      max thread num initialized in pool 
  27.  */  
  28. typedef struct thread_pool{  
  29.     pthread_mutex_t pool_lock;  
  30.     pthread_cond_t  job_ready;  
  31.     Job * jobs;  
  32.     int destroy;  
  33.     pthread_t * threads;  
  34.     unsigned int thread_num;  
  35.     int size;  
  36. }Thread_pool;  
  37. /* global Thread_pool variable*/  
  38. static Thread_pool * pool=NULL;  
  39.   
  40. /*Initialize the thread pool*/  
  41. int pool_init(unsigned int thread_num)  
  42. {  
  43.     pool=(Thread_pool *)malloc(sizeof(Thread_pool));  
  44.     if(NULL==pool)  
  45.         return -1;  
  46.     pthread_mutex_init(&(pool->pool_lock),NULL);  
  47.     pthread_cond_init(&(pool->job_ready),NULL);  
  48.     pool->jobs=NULL;  
  49.     pool->thread_num=thread_num;  
  50.     pool->size=0;  
  51.     pool->destroy=0;  
  52.     pool->threads=(pthread_t *)malloc(thread_num * sizeof(pthread_t));  
  53.       
  54.     int i;  
  55.     for(i=0;i<thread_num;i++){  
  56.         pthread_create(&(pool->threads[i]),NULL,routine,NULL);  
  57.     }  
  58.     return 0;  
  59. }  
  60. /* 
  61.  * Add job into the pool 
  62.  * assign it to some thread 
  63.  */  
  64. int pool_add_job(void *(*process)(void *),void *arg)  
  65. {  
  66.     Job * newjob=(Job *)malloc(sizeof(Job));  
  67.     newjob->process=process;  
  68.     newjob->arg=arg;  
  69.     newjob->next=NULL;  
  70.     pthread_mutex_lock(&(pool->pool_lock));  
  71.     Job * temp=pool->jobs;  
  72.     if(temp!=NULL){  
  73.         while(temp->next)  
  74.             temp=temp->next;  
  75.         temp->next=newjob;  
  76.     }  
  77.     else{  
  78.         pool->jobs=newjob;  
  79.     }  
  80.       
  81.     /*For debug*/  
  82.     assert(pool->jobs!=NULL);  
  83.     pool->size++;  
  84.     pthread_mutex_unlock(&(pool->pool_lock));  
  85.     /*Rouse a thread to process this new job*/  
  86.     pthread_cond_signal(&(pool->job_ready));  
  87.     return 0;  
  88. }  
  89. /*Destroy the thread pool*/  
  90. int pool_destroy(void)  
  91. {  
  92.     if(pool->destroy)/*Alread destroyed!*/  
  93.         return -1;  
  94.     int i;  
  95.     pool->destroy=1;  
  96.     pthread_cond_broadcast(&(pool->job_ready));/*notify all threads*/  
  97.     for(i=0;i<pool->thread_num;i++)  
  98.         pthread_join(pool->threads[i],NULL);  
  99.     free(pool->threads);  
  100.     Job * head=NULL;  
  101.     while(pool->jobs){  
  102.         head=pool->jobs;  
  103.         pool->jobs=pool->jobs->next;  
  104.         free(head);  
  105.     }  
  106.     pthread_mutex_destroy(&(pool->pool_lock));  
  107.     pthread_cond_destroy(&(pool->job_ready));  
  108.     free(pool);  
  109.     pool=NULL;  
  110.     return 0;  
  111. }  
  112. /*Every thread call this function*/  
  113. void * routine(void *arg)  
  114. {  
  115.     printf("start thread %u/n",pthread_self());  
  116.     while(1){  
  117.         pthread_mutex_lock(&(pool->pool_lock));  
  118.         while(pool->size==0 && !pool->destroy){  
  119.             printf("thread %u is waiting/n",pthread_self());  
  120.             pthread_cond_wait(&(pool->job_ready),&(pool->pool_lock));  
  121.         }  
  122.         if(pool->destroy){  
  123.             pthread_mutex_unlock(&(pool->pool_lock));  
  124.             printf("thread %u will exit/n",pthread_self());  
  125.             pthread_exit(NULL);  
  126.         }  
  127.         printf("thread %u is starting to work/n",pthread_self());  
  128.         /*For debug*/  
  129.         assert(pool->size!=0);  
  130.         assert(pool->jobs!=NULL);  
  131.         pool->size--;  
  132.         Job * job=pool->jobs;  
  133.         pool->jobs=job->next;  
  134.         pthread_mutex_unlock(&(pool->pool_lock));  
  135.         /*process job*/  
  136.         (*(job->process))(job->arg);  
  137.         free(job);  
  138.         job=NULL;  
  139.     }  
  140.     /*You should never get here*/  
  141.     pthread_exit(NULL);  
  142. }  
  143. void *test(void *arg)  
  144. {  
  145.     printf("thread %u is working on job %u/n",pthread_self(),*(int *)arg);  
  146.     sleep(1);  
  147.       
  148.     return NULL;  
  149. }  
  150. int main(int argc,char **argv)  
  151. {  
  152.     int i;  
  153.     int *jobs=(int *)malloc(sizeof(int)*10);  
  154.     pool_init(3);  
  155.     for(i=0;i<10;i++){  
  156.         jobs[i]=i;  
  157.         pool_add_job(test,&jobs[i]);  
  158.     }  
  159.     sleep(5);  
  160.     pool_destroy();  
  161.     free(jobs);  
  162.     return EXIT_SUCCESS;  
  163. }   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值