Linux下c语言线程池的实现
来源:互联网 发布:阿里云备案网站负责人 编辑:程序博客网 时间:2024/05/16 05:20
1.线程池基本原理
1)线程管理器:用于创建并管理线程池。
2)工作线程:线程池中实际执行任务的线程。在初始化线程时会预先创建好固定数目的线程在池中,这些初始化的线程一般处于空闲状态,一般不占用CPU,占用较小的内存空间。
3)任务接口:每个任务必须实现的接口,当线程池的任务队列中有可执行任务时,被空闲的工作线程调去执行(线程的闲与忙是通过互斥量实现的,跟前面文章中的设置标志位差不多),把任务抽象出来形成接口,可以做到线程池与具体的任务无关。
4)任务队列:用来存放没有处理的任务,提供一种缓冲机制,实现这种结构有好几种方法,常用的是队列,主要运用先进先出原理,另外一种是链表之类的数据结构,可以动态的为它分配内存空间,应用中比较灵活,下文中就是用到的链表。
下面的不在赘述百度《线程池技术在并发服务器中的应用》写的非常详细!
转自:http://blog.csdn.net/zouxinfox/article/details/3560891
- while
(pool->cur_queue_size == 0) - {
-
pthread_cond_wait (&(pool->queue_ready),&(pool->queue_lock)); - }
- #include
- #include
- #include
- #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_size; -
- }
CThread_pool; -
-
-
- int
pool_add_worker void( *(*process) void( *arg), void*arg); - void
*thread_routine void( *arg); -
-
- //share
resource - static
CThread_pool *pool = NULL; - void
- pool_init
(int max_thread_num) - {
-
pool = (CThread_pool *) malloc (sizeof (CThread_pool)); -
-
pthread_mutex_init (&(pool->queue_lock), NULL); -
pthread_cond_init (&(pool->queue_ready), NULL); -
-
pool->queue_head = NULL; -
-
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 < max_thread_num; i++) -
{ -
pthread_create (&(pool->threadid[i]), NULL, thread_routine,NULL); -
} - }
-
-
-
-
- int
- pool_add_worker
(void *(*process) void( *arg), void*arg) - {
-
-
CThread_worker *newworker = (CThread_worker *) malloc (sizeof (CThread_worker)); -
newworker->process = process; -
newworker->arg = arg; -
newworker->next = NULL; -
-
pthread_mutex_lock (&(pool->queue_lock)); -
-
CThread_worker *member = pool->queue_head; -
if (member != NULL) -
{ -
while (member->next != NULL) -
member = member->next; -
member->next = newworker; -
} -
else -
{ -
pool->queue_head = newworker; -
} -
-
assert (pool->queue_head != NULL); -
-
pool->cur_queue_size++; -
pthread_mutex_unlock (&(pool->queue_lock)); -
-
pthread_cond_signal (&(pool->queue_ready)); -
return 0; - }
-
-
-
-
- int
- pool_destroy
() - {
-
if (pool->shutdown) -
return -1; -
pool->shutdown = 1; -
-
-
pthread_cond_broadcast (&(pool->queue_ready)); -
-
-
int i; -
for (i = 0; i < pool->max_thread_num; i++) -
pthread_join (pool->threadid[i], 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(&(pool->queue_lock)); -
pthread_cond_destroy(&(pool->queue_ready)); -
-
free (pool); -
-
pool=NULL; -
return 0; - }
-
-
-
- void
* - thread_routine
(void *arg) - {
-
printf ("starting thread ,0x%x\n" pthread_self ()); -
while (1) -
{ -
pthread_mutex_lock (&(pool->queue_lock)); -
-
while (pool->cur_queue_size == 0 && !pool->shutdown) -
{ -
printf ("thread 0x%x ,is waiting\n" pthread_self ()); -
pthread_cond_wait (&(pool->queue_ready), &(pool->queue_lock)); -
} -
-
-
if (pool->shutdown) -
{ -
-
pthread_mutex_unlock (&(pool->queue_lock)); -
printf ("thread 0x%x ,will exit\n" pthread_self ()); -
pthread_exit (NULL); -
} -
-
printf ("thread 0x%x ,is starting to work\n" 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 (&(pool->queue_lock)); -
-
-
(*(worker->process)) (worker->arg); -
free (worker); -
worker = NULL; -
} -
-
pthread_exit (NULL); - }
-
- //
下面是测试代码 -
- void
* - myprocess
(void *arg) - {
-
printf ("threadid is ,0x%x, working on task %d\n" pthread_self int(),*( *) arg); -
sleep (1); -
return NULL; - }
-
- int
- main
(int argc, char**argv) - {
-
pool_init (3); -
-
-
int *workingnum int= ( *) sizeofmalloc ( ( int)* 10); -
int i; -
for (i = 0; i < 10; i++) -
{ -
workingnum[i] = i; -
pool_add_worker (myprocess, &workingnum[i]); -
} -
-
sleep (5); -
-
pool_destroy (); -
-
free (workingnum); -
return 0; - }
将上述所有代码放入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
- Linux下C语言线程池的实现(1)
- Linux下c语言线程池的实现
- 一个Linux下c语言线程池的实现
- Linux下的C语言线程池
- linux线程池的C语言实现
- linux线程池的C语言实现
- linux线程池的C语言实现
- linux线程池的C语言实现
- linux线程池的C语言实现
- linux线程池的C语言实现
- Linux下C线程池的实现
- Linux下C线程池的实现
- Linux下C线程池的实现
- Linux下C线程池的实现
- Linux下C线程池的实现
- Linux下C线程池的实现
- Linux下C线程池的实现
- linux下C语言实现一个线程池
- 如何实现JMeter关联
- 堆(heap)和栈(stack)的区别
- JAVA学习代码——实现文件名的过滤
- leetcode之求组合数
- Source Insight 3.x serial 序列号
- Linux下c语言线程池的实现
- IOCP不可忽视的细节
- 为什么用iso-8859-1来转换字符集
- NoSQL_Cassandra_教程四_搭建Cassandra的数据库集群
- 股票 趋势
- 编写一个Java程序,用if-else语句判断某年份是否为闰年。
- nodeJS之http搭建服务端和客户端
- web开发中css选择器使用
- Hbase shell常用命令总结