linux下C语言实现一个线程池.doc_第1页
linux下C语言实现一个线程池.doc_第2页
linux下C语言实现一个线程池.doc_第3页
linux下C语言实现一个线程池.doc_第4页
linux下C语言实现一个线程池.doc_第5页
已阅读5页,还剩2页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

linux下C语言实现一个线程池 (2012-04-14 15:20:20)转载1 什么时候需要创建线程池呢?简单的说,如果一个应用需要频繁的创建和销毁线程,而任务执行的时间又非常短,这样线程创建和销毁的带来的开销就不容忽视,这时也是线程池该出场的机会了。如果线程创建和销毁时间相比任务执行时间可以忽略不计,则没有必要使用线程池了。 下面是Linux系统下用C语言创建的一个线程池。线程池会维护一个任务链表(每个CThread_worker结构就是一个任务)。 pool_init()函数预先创建好max_thread_num个线程,每个线程执thread_routine ()函数。该函数中while (pool-cur_queue_size = 0)2 3 pthread_cond_wait (&(pool-queue_ready),&(pool-queue_lock);4 5 表示如果任务链表中没有任务,则该线程出于阻塞等待状态。否则从队列中取出任务并执行。 pool_add_worker()函数向线程池的任务链表中加入一个任务,加入后通过调用pthread_cond_signal (&(pool-queue_ready)唤醒一个出于阻塞状态的线程(如果有的话)。 pool_destroy ()函数用于销毁线程池,线程池任务链表中的任务不会再被执行,但是正在运行的线程会一直把任务运行完后再退出。下面贴出完整代码#include 6 #include 7 #include 8 #include 9 #include 10 #include 1112 typedef struct worker13 14 15 void *(*process) (void *arg);16 void *arg;17 struct worker *next;1819 CThread_worker;202122 typedef struct23 24 pthread_mutex_t queue_lock;25 pthread_cond_t queue_ready;2627 28 CThread_worker *queue_head;2930 31 int shutdown;32 pthread_t *threadid;33 34 int max_thread_num;35 36 int cur_queue_size;3738 CThread_pool;394041 int pool_add_worker (void *(*process) (void *arg), void*arg);42 void *thread_routine (void *arg);434445 static CThread_pool *pool = NULL;46 void47 pool_init (int max_thread_num)48 49 pool = (CThread_pool *) malloc (sizeof(CThread_pool);5051 pthread_mutex_init (&(pool-queue_lock), NULL);52 pthread_cond_init (&(pool-queue_ready), NULL);5354 pool-queue_head = NULL;5556 pool-max_thread_num = max_thread_num;57 pool-cur_queue_size = 0;5859 pool-shutdown = 0;6061 pool-threadid =62 (pthread_t *) malloc (max_thread_num * sizeof(pthread_t);63 int i = 0;64 for (i = 0; i threadidi), NULL, thread_routine,67 NULL);68 69 707172 int73 pool_add_worker (void *(*process) (void *arg), void*arg)74 75 76 CThread_worker *newworker =77 (CThread_worker *) malloc (sizeof(CThread_worker);78 newworker-process = process;79 newworker-arg = arg;80 newworker-next = NULL;8182 pthread_mutex_lock (&(pool-queue_lock);83 84 CThread_worker *member = pool-queue_head;85 if (member != NULL)86 87 while (member-next != NULL)88 member = member-next;89 member-next = newworker;90 91 else92 93 pool-queue_head = newworker;94 9596 assert (pool-queue_head != NULL);9798 pool-cur_queue_size+;99 pthread_mutex_unlock (&(pool-queue_lock);100 101 pthread_cond_signal (&(pool-queue_ready);102 return 0;103 104105106 int107 pool_destroy ()108 109 if (pool-shutdown)110 return -1;111 pool-shutdown = 1;112113 114 pthread_cond_broadcast (&(pool-queue_ready);115116 117 int i;118 for (i = 0; i max_thread_num; i+)119 pthread_join (pool-threadidi, NULL);120 free (pool-threadid);121122 123 CThread_worker *head = NULL;124 while (pool-queue_head != NULL)125 126 head = pool-queue_head;127 pool-queue_head = pool-queue_head-next;128 free (head);129 130 131 pthread_mutex_destroy(&(pool-queue_lock);132 pthread_cond_destroy(&(pool-queue_ready);133 134 free (pool);135 136 pool=NULL;137 return 0;138 139140141 void *142 thread_routine (void *arg)143 144 printf (starting thread 0x%xn, pthread_self ();145 while (1)146 147 pthread_mutex_lock (&(pool-queue_lock);148 149 while (pool-cur_queue_size = 0 & !pool-shutdown)150 151 printf (thread 0x%x is waitingn, pthread_self ();152 pthread_cond_wait (&(pool-queue_ready), &(pool-queue_lock);153 154155 156 if (pool-shutdown)157 158 159 pthread_mutex_unlock (&(pool-queue_lock);160 printf (thread 0x%x will exitn, pthread_self ();161 pthread_exit (NULL);162 163164 printf (thread 0x%x is starting to workn, pthread_self ();165166 167 assert (pool-cur_queue_size != 0);168 assert (pool-queue_head != NULL);169 170 171 pool-cur_queue_size-;172 CThread_worker *worker = pool-queue_head;173 pool-queue_head = worker-next;174 pthread_mutex_unlock (&(pool-queue_lock);175176 177 (*(worker-process) (worker-arg);178 free (worker);179 worker = NULL;180 181 182 pthread_exit (NULL);183 184185 下面是测试代码void *186 myprocess (void *arg)187 188 printf (threadid is 0x%x, working on task %dn, pthread_self (),*(int *) arg);189 sleep (1);190 return NULL;191 192193 int194 main (int argc, char *argv)195 196 pool_init (3);197 198 199 int *workingnum = (int *) malloc (sizeof (int) * 10);200 int i;201 for (i = 0; i 10; i+)202 203 workingnumi = i;204 pool_add_worker (myprocess, &workingnumi);205 206 207 sleep (5);208 209 pool_destroy ();210211 free (workingnum);212 return 0;213 将上述所有代码放入threadpool.c文件中,在Linux输入编译命令$ gcc -o threadpool threadpool.c -lpthread以下是运行结果starting thread 0xb7df6b90thread 0xb7df6b90 is waitingstarting thread 0xb75f5b90thread 0xb75f5b90 is waitingstarting thread 0xb6df4b90thread 0xb6df4b90 is waitingthread 0xb7df6b90 is starting to workthreadid is 0xb7df6b90, working on task 0thread 0xb75f5b90 is starting to workthreadid is 0xb75f5b90, working on task 1thread 0xb6df4b90 is starting to workthreadid is 0xb6df4b90, working on task 2thread 0xb7df6b90 is starting to workthreadid is 0xb7df6b90, working on task 3thread 0xb75f5b90 is starting to workthreadid is 0xb75f5b90, working on task 4thread 0xb6df4b90 is starting to workthreadid is 0xb6df4b90, working on task 5thread 0xb7df6b90 is starting to workthreadid is 0xb7df6b90, working on task 6thread 0xb75f5b90 is starting to workthreadid is 0xb75f5b90, working on task 7thread 0xb6df4b90 is starting to workthreadid i

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论