linux下c语言实现一个线程池

上传人:第*** 文档编号:32688017 上传时间:2018-02-12 格式:DOC 页数:7 大小:92.50KB
返回 下载 相关 举报
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 语言实现一个线程池(2012-04-14 15:20:20)转 载 什么时候需要创建线程池呢?简单的说,如果一个应用需要频繁的 创建和销毁线程,而任务执行的时间又非常短,这样线程创建和销毁的带来的开销就不容忽视,这时也是线程池该出场的机会了。如果线程创建和销毁时间相比任务执行时间可以忽略不计,则没有必要使用线程池了。下面是 Linux 系统下用 C 语言创建的一个线程池。线程池会维护一个任务链表(每个 CThread_worker结构就是一个任务)。pool_init()函数预先创建好 max_thread_num 个线程,每个线程执 thread_routine ()函数

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

3、ude #include #include #include typedef struct worker void *(*process) (void *arg); void *arg; struct worker *next; CThread_worker; typedef struct pthread_mutex_t queue_lock; pthread_cond_t queue_ready; CThread_worker *queue_head; int shutdown; pthread_t *threadid; int max_thread_num; int cur_queue_s

4、ize; CThread_pool; int pool_add_worker (void *(*process) (void *arg), void*arg); void *thread_routine (void *arg); static CThread_pool *pool = NULL; void pool_init (int max_thread_num) pool = (CThread_pool *) malloc (sizeof(CThread_pool); pthread_mutex_init ( pthread_cond_init ( pool-queue_head = NU

5、LL; pool-max_thread_num = max_thread_num; pool-cur_queue_size = 0; pool-shutdown = 0; pool-threadid = (pthread_t *) malloc (max_thread_num * sizeof(pthread_t); int i = 0; for (i = 0; i threadidi), NULL, thread_routine, NULL); int pool_add_worker (void *(*process) (void *arg), void*arg) CThread_worke

6、r *newworker = (CThread_worker *) malloc (sizeof(CThread_worker); newworker-process = process; newworker-arg = arg; newworker-next = NULL; pthread_mutex_lock ( CThread_worker *member = pool-queue_head; if (member != NULL) while (member-next != NULL) member = member-next; member-next = newworker; els

7、e pool-queue_head = newworker; assert (pool-queue_head != NULL); pool-cur_queue_size+; pthread_mutex_unlock ( pthread_cond_signal ( return 0;intpool_destroy () if (pool-shutdown) return -1; pool-shutdown = 1; pthread_cond_broadcast ( int i; for (i = 0; i max_thread_num; i+) pthread_join (pool-thread

8、idi, NULL); free (pool-threadid); CThread_worker *head = NULL; while (pool-queue_head != NULL) head = pool-queue_head; pool-queue_head = pool-queue_head-next; free (head); pthread_mutex_destroy( pthread_cond_destroy( free (pool); pool=NULL; return 0;void *thread_routine (void *arg) printf (starting

9、thread 0x%xn, pthread_self (); while (1) pthread_mutex_lock ( while (pool-cur_queue_size = 0 & !pool-shutdown) printf (thread 0x%x is waitingn, pthread_self (); pthread_cond_wait (&(pool-queue_ready), &(pool-queue_lock); if (pool-shutdown) pthread_mutex_unlock ( printf (thread 0x%x will exitn, pthre

10、ad_self (); pthread_exit (NULL); printf (thread 0x%x is starting to workn, pthread_self (); assert (pool-cur_queue_size != 0); assert (pool-queue_head != NULL); pool-cur_queue_size-; CThread_worker *worker = pool-queue_head; pool-queue_head = worker-next; pthread_mutex_unlock ( (*(worker-process) (w

11、orker-arg); free (worker); worker = NULL; pthread_exit (NULL); 下面是测试代码 void *myprocess (void *arg) printf (threadid is 0x%x, working on task %dn, pthread_self (),*(int *) arg); sleep (1); return NULL;intmain (int argc, char *argv) pool_init (3); int *workingnum = (int *) malloc (sizeof (int) * 10); int i; for (i = 0; i 10; i+) workingnumi = i; pool_add_worker (myprocess, sleep (5); pool_destroy (); free (workingnum); return 0;将上述所有代码放入 threadpool.c 文件中,在 Linux 输入编译命令$ gcc -o threadpool threadpool.c -lpthread以下是运行结果sta

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

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

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