第一范文网 - 专业文章范例文档资料分享平台

线程池

来源:用户分享 时间:2025/5/31 6:14:56 本文由loading 分享 下载这篇文档手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:xxxxxxx或QQ:xxxxxx 处理(尽可能给您提供完整文档),感谢您的支持与谅解。

121.

122. /*阻塞等待线程退出,否则就成僵尸了*/ 123. int i;

124. for (i = 0; i < pool->max_thread_num; i++) 125. pthread_join (pool->threadid[i], NULL); 126. free (pool->threadid); 127.

128. /*销毁等待队列*/

129. CThread_worker *head = NULL; 130. while (pool->queue_head != NULL) 131. {

132. head = pool->queue_head;

133. pool->queue_head = pool->queue_head->next; 134. free (head); 135. }

136. /*条件变量和互斥量也别忘了销毁*/

137. pthread_mutex_destroy(&(pool->queue_lock)); 138. pthread_cond_destroy(&(pool->queue_ready)); 139.

140. free (pool);

141. /*销毁后指针置空是个好习惯*/ 142. pool=NULL; 143. return 0; 144. } 145. 146. 147.

148. void *

149. thread_routine (void *arg) 150. {

151. printf (\, pthread_self ()); 152. while (1) 153. {

154. pthread_mutex_lock (&(pool->queue_lock));

155. /*如果等待队列为0并且不销毁线程池,则处于阻塞状态; 注意

156. pthread_cond_wait是一个原子操作,等待前会解锁,唤醒后会加锁*/ 157. while (pool->cur_queue_size == 0 && !pool->shutdown) 158. {

159. printf (\, pthread_self ()); 160. pthread_cond_wait (&(pool->queue_ready), &(pool->queue_lock)); 161. } 162.

163. /*线程池要销毁了*/

164. if (pool->shutdown) 165. {

166. /*遇到break,continue,return等跳转语句,千万不要忘记先解锁*/

167. pthread_mutex_unlock (&(pool->queue_lock));

168. printf (\, pthread_self ()); 169. pthread_exit (NULL); 170. } 171.

172. printf (\, pthread_self ()); 173.

174. /*assert是调试的好帮手*/

175. assert (pool->cur_queue_size != 0); 176. assert (pool->queue_head != NULL); 177.

178. /*等待队列长度减去1,并取出链表中的头元素*/ 179. pool->cur_queue_size--;

180. CThread_worker *worker = pool->queue_head; 181. pool->queue_head = worker->next;

182. pthread_mutex_unlock (&(pool->queue_lock)); 183.

184. /*调用回调函数,执行任务*/

185. (*(worker->process)) (worker->arg); 186. free (worker); 187. worker = NULL; 188. }

189. /*这一句应该是不可达的*/ 190. pthread_exit (NULL); 191. } 192.

193. // 下面是测试代码 194.

195. void *

196. myprocess (void *arg) 197. {

198. printf (\, pthread_self (),*(int

*) arg);

199. sleep (1);/*休息一秒,延长任务的执行时间*/ 200. return NULL; 201. } 202. 203. int

204. main (int argc, char **argv) 205. {

206. pool_init (3);/*线程池中最多三个活动线程*/ 207.

208. /*连续向池中投入10个任务*/

209. int *workingnum = (int *) malloc (sizeof (int) * 10); 210. int i;

211. for (i = 0; i < 10; i++) 212. {

213. workingnum[i] = i;

214. pool_add_worker (myprocess, &workingnum[i]); 215. }

216. /*等待所有任务完成*/ 217. sleep (5); 218. /*销毁线程池*/ 219. pool_destroy (); 220.

221. free (workingnum); 222. return 0; 223. }

将上述所有代码放入threadpool.c文件中, 在Linux输入编译命令 $ gcc -o threadpool threadpool.c -lpthread 以下是运行结果 starting thread 0xb7df6b90 thread 0xb7df6b90 is waiting starting thread 0xb75f5b90 thread 0xb75f5b90 is waiting starting thread 0xb6df4b90 thread 0xb6df4b90 is waiting thread 0xb7df6b90 is starting to work threadid is 0xb7df6b90, working on task 0 thread 0xb75f5b90 is starting to work threadid is 0xb75f5b90, working on task 1 thread 0xb6df4b90 is starting to work threadid is 0xb6df4b90, working on task 2 thread 0xb7df6b90 is starting to work threadid is 0xb7df6b90, working on task 3 thread 0xb75f5b90 is starting to work threadid is 0xb75f5b90, working on task 4 thread 0xb6df4b90 is starting to work threadid is 0xb6df4b90, working on task 5 thread 0xb7df6b90 is starting to work threadid is 0xb7df6b90, working on task 6 thread 0xb75f5b90 is starting to work threadid is 0xb75f5b90, working on task 7 thread 0xb6df4b90 is starting to work threadid is 0xb6df4b90, working on task 8 thread 0xb7df6b90 is starting to work threadid is 0xb7df6b90, working on task 9 thread 0xb75f5b90 is waiting thread 0xb6df4b90 is waiting thread 0xb7df6b90 is waiting thread 0xb75f5b90 will exit thread 0xb6df4b90 will exit thread 0xb7df6b90 will exit

搜索更多关于: 线程池 的文档
线程池.doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.diyifanwen.net/c9bguu6i4i99da6b52iwg_2.html(转载请注明文章来源)
热门推荐
Copyright © 2012-2023 第一范文网 版权所有 免责声明 | 联系我们
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:xxxxxx 邮箱:xxxxxx@qq.com
渝ICP备2023013149号
Top