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
相关推荐: