linux 线程池 c 实现

上传人:第*** 文档编号:32687959 上传时间:2018-02-12 格式:DOC 页数:7 大小:48KB
返回 下载 相关 举报
linux 线程池 c 实现_第1页
第1页 / 共7页
linux 线程池 c 实现_第2页
第2页 / 共7页
linux 线程池 c 实现_第3页
第3页 / 共7页
linux 线程池 c 实现_第4页
第4页 / 共7页
linux 线程池 c 实现_第5页
第5页 / 共7页
点击查看更多>>
资源描述

《linux 线程池 c 实现》由会员分享,可在线阅读,更多相关《linux 线程池 c 实现(7页珍藏版)》请在金锄头文库上搜索。

1、一个 Linux 下 C 线程池的实现2009-01-05 21:18什么时候需要创建线程池呢?简单的说,如果一个应用需要频繁的创建和销毁线程,而任务执行的时间又非常短,这样线程创建和销毁的带来的开销就不容忽视,这时也是线程池该出场的机会了。如果线程创建和销毁时间相比任务执行时间可以忽略不计,则没有必要使用线程池了。下面是 Linux 系统下用 C 语言创建的一个线程池。线程池会维护一个任务链表(每个 CThread_worker 结构就是一个任务)。pool_init()函数预先创建好 max_thread_num 个线程,每个线程执thread_routine ()函数。该函数中1. wh

2、ile (pool-cur_queue_size = 0) 2. 3. pthread_cond_wait (&(pool-queue_ready),&(pool-queue_lock); 4. 表示如果任务链表中没有任务,则该线程出于阻塞等待状态。否则从队列中取出任务并执行。pool_add_worker()函数向线程池的任务链表中加入一个任务,加入后通过调用 pthread_cond_signal (&(pool-queue_ready)唤醒一个出于阻塞状态的线程(如果有的话)。pool_destroy ()函数用于销毁线程池,线程池任务链表中的任务不会再被执行,但是正在运行的线程会一直把

3、任务运行完后再退出。下面贴出完整代码1. #include 2. #include 3. #include 4. #include 5. #include 6. #include 7.8. /* 9. *线程池里所有运行和等待的任务都是一个 CThread_worker 10.*由于所有任务都在链表里,所以是一个链表结构 11.*/ 12.typedef struct worker 13. 14. /*回调函数,任务运行时会调用此函数,注意也可声明成其它形式*/ 15. void *(*process) (void *arg); 16. void *arg;/*回调函数的参数*/ 17. st

4、ruct worker *next; 18.19. CThread_worker; 20.21.22./*线程池结构*/ 23.typedef struct 24. 25. pthread_mutex_t queue_lock; 26. pthread_cond_t queue_ready; 27.28. /*链表结构,线程池中所有等待任务*/ 29. CThread_worker *queue_head; 30.31. /*是否销毁线程池*/ 32. int shutdown; 33. pthread_t *threadid; 34. /*线程池中允许的活动线程数目*/ 35. int ma

5、x_thread_num; 36. /*当前等待队列的任务数目*/ 37. int cur_queue_size; 38.39. CThread_pool; 40.41.42.int pool_add_worker (void *(*process) (void *arg), void *arg); 43.void *thread_routine (void *arg); 44.45.46.static CThread_pool *pool = NULL; 47.void 48.pool_init (int max_thread_num) 49. 50. pool = (CThread_poo

6、l *) malloc (sizeof (CThread_pool); 51.52. pthread_mutex_init ( 53. pthread_cond_init ( 54.55. pool-queue_head = NULL; 56.57. pool-max_thread_num = max_thread_num; 58. pool-cur_queue_size = 0; 59.60. pool-shutdown = 0; 61.62. pool-threadid = 63. (pthread_t *) malloc (max_thread_num * sizeof (pthread

7、_t); 64. int i = 0; 65. for (i = 0; i threadidi), NULL, thread_routine, 68. NULL); 69. 70. 71.72.73./*向线程池中加入任务*/ 74.int 75.pool_add_worker (void *(*process) (void *arg), void *arg) 76. 77. /*构造一个新任务*/ 78. CThread_worker *newworker = 79. (CThread_worker *) malloc (sizeof (CThread_worker); 80. newwor

8、ker-process = process; 81. newworker-arg = arg; 82. newworker-next = NULL;/*别忘置空*/ 83.84. pthread_mutex_lock ( 85. /*将任务加入到等待队列中*/ 86. CThread_worker *member = pool-queue_head; 87. if (member != NULL) 88. 89. while (member-next != NULL) 90. member = member-next; 91. member-next = newworker; 92. 93.

9、else 94. 95. pool-queue_head = newworker; 96. 97.98. assert (pool-queue_head != NULL); 99.100. pool-cur_queue_size+; 101. pthread_mutex_unlock ( 102. /*好了,等待队列中有任务了,唤醒一个等待线程; 103. 注意如果所有线程都在忙碌,这句没有任何作用*/ 104. pthread_cond_signal ( 105. return 0; 106. 107.108.109. /*销毁线程池,等待队列中的任务不会再被执行,但是正在运行的线程会一直

10、110. 把任务运行完后再退出*/ 111. int 112. pool_destroy () 113. 114. if (pool-shutdown) 115. return -1;/*防止两次调用*/ 116. pool-shutdown = 1; 117.118. /*唤醒所有等待线程,线程池要销毁了*/ 119. pthread_cond_broadcast ( 120.121. /*阻塞等待线程退出,否则就成僵尸了*/ 122. int i; 123. for (i = 0; i max_thread_num; i+) 124. pthread_join (pool-threadid

11、i, NULL); 125. free (pool-threadid); 126.127. /*销毁等待队列*/ 128. CThread_worker *head = NULL; 129. while (pool-queue_head != NULL) 130. 131. head = pool-queue_head; 132. pool-queue_head = pool-queue_head-next; 133. free (head); 134. 135. /*条件变量和互斥量也别忘了销毁*/ 136. pthread_mutex_destroy( 137. pthread_cond_

12、destroy( 138. 139. free (pool); 140. /*销毁后指针置空是个好习惯*/ 141. pool=NULL; 142. return 0; 143. 144.145.146. void * 147. thread_routine (void *arg) 148. 149. printf (starting thread 0x%xn, pthread_self (); 150. while (1) 151. 152. pthread_mutex_lock ( 153. /*如果等待队列为 0 并且不销毁线程池,则处于阻塞状态; 注意 154. pthread_con

13、d_wait 是一个原子操作,等待前会解锁,唤醒后会加锁*/ 155. while (pool-cur_queue_size = 0 & !pool-shutdown) 156. 157. printf (thread 0x%x is waitingn, pthread_self (); 158. pthread_cond_wait (&(pool-queue_ready), 159. 160.161. /*线程池要销毁了*/ 162. if (pool-shutdown) 163. 164. /*遇到 break,continue,return 等跳转语句,千万不要忘记先解锁*/ 165.

14、pthread_mutex_unlock ( 166. printf (thread 0x%x will exitn, pthread_self (); 167. pthread_exit (NULL); 168. 169.170. printf (thread 0x%x is starting to workn, pthread_self (); 171.172. /*assert 是调试的好帮手*/ 173. assert (pool-cur_queue_size != 0); 174. assert (pool-queue_head != NULL); 175. 176. /*等待队列长度减去 1,并取出链表中的头元素*/ 177. pool-cur_queue_s

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 中学教育 > 职业教育

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号