linux学习:线程池

目录

原理

初始线程池

运行中的线程池

相关结构体

api

线程池初始化

投送任务

增加活跃线程

删除活跃线程

销毁线程池

例子

thread_pool.h

thread_pool.c

test.c  测试程序


原理

一个进程中的线程就好比是一家公司里的员工,员工的数目应该根据公司的业务多少来 定,太少了忙不过来,但是太多了也浪费资源,最理想的情况是:让进程有一些初始数目的 线程(所谓的线程池),当没有任务的时候这些线程自动进入睡眠,有了任务他们会立即执 行任务,不断循环。进程还应该可以根据自身任务的繁重与否来增删线程的数目,当所有的 任务都完成了之后,所有的线程还能妥当地收官走人,不带走一片云彩

初始线程池

  • 任务队列中刚开始没有任何任务,是一个具有头结点的空链队列
  • 使用互斥锁来保护这个队列
  • 使用条件变量来代表任务队列中的任务个数的变化——将来如果主线程往队列中投 放任务,那么可以通过条件变量来唤醒那些睡着了的线程
  • 通过一个公共开关——shutdown,来控制线程退出,进而销毁整个线程池

运行中的线程池

相关结构体

任务节点,包含需要执行的函数及其参数,通过链表连成一个任务队列

api

线程池初始化

投送任务

增加活跃线程

删除活跃线程

销毁线程池

例子

thread_pool.h

1 #ifndef _THREAD_POOL_H_
2 #define _THREAD_POOL_H_
3
4 #include <stdio.h>
5 #include <stdbool.h>
6 #include <unistd.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <strings.h>
10
11 #include <errno.h>
12 #include <pthread.h>
13
14 #define MAX_WAITING_TASKS 1000 //最大等待任务数
15 #define MAX_ACTIVE_THREADS 20  //最大活跃线程数的常量
16
17 struct task // 任务节点结构体
18 {
19     void *(*task)(void *arg);
20     void *arg;
21
22     struct task *next;
23 };
24
25 typedef struct thread_pool // 线程池结构体
26 {
27     pthread_mutex_t lock;
28     pthread_cond_t cond;
29     struct task *task_list;
30
31     pthread_t *tids;
32
33     unsigned waiting_tasks;
34     unsigned active_threads;
35
36     bool shutdown;
37 }thread_pool;
38
39 // 线程池初始化
40 
41 bool init_pool(thread_pool *pool, 
42         unsigned int threads_number);
43 // 投放任务
44 
45 bool add_task(thread_pool *pool, 
46         void *(*task)(void *arg), 
47             void *arg);
48 // 增加线程
49 
50 int add_thread(thread_pool *pool, 
51         unsigned int additional_threads_number);
52 // 删除线程
53 
54 int remove_thread(thread_pool *pool, 
55         unsigned int removing_threads_number);
56
57 bool destroy_pool(thread_pool *pool); // 销毁线程池
58 void *routine(void *arg); // 线程例程
59
60 #endif

thread_pool.c

1 #include "thread_pool.h" 
2 // 函数在被取消时调用,用于释放互斥锁
3 void handler(void *arg)
4 {
5     // 响应取消请求之后自动处理的例程:释放互斥锁
6     pthread_mutex_unlock((pthread_mutex_t *)arg);
7 }
8 // 函数是线程的执行例程,不断地从任务队列中取出任务并执行
9 void *routine(void *arg)
10 {
11     thread_pool *pool = (thread_pool *)arg;
12     struct task *p;
13
14     while(1)
15     {
16         // 访问任务队列前加锁,为防止取消后死锁,注册处理例程 handler
17         pthread_cleanup_push(handler, (void *)&pool->lock);
18         pthread_mutex_lock(&pool->lock);
19
20         // 若当前没有任务,且线程池未关闭,则进入条件变量等待队列睡眠
21         while(pool->waiting_tasks == 0 && !pool->shutdown)
22         {
23             pthread_cond_wait(&pool->cond, &pool->lock);
24         }
25
26         // 若当前没有任务,且线程池关闭标识为真,则立即释放互斥锁并退出
27         if(pool->waiting_tasks == 0 && pool->shutdown == true)
28         {
29             pthread_mutex_unlock(&pool->lock);
30             pthread_exit(NULL);
31         }
32
33         // 若当前有任务,则消费任务队列中的任务
34         p = pool->task_list->next;
35         pool->task_list->next = p->next;
36         pool->waiting_tasks--;
37
38         // 释放互斥锁,并弹栈 handler(但不执行他)
39         pthread_mutex_unlock(&pool->lock);
40         pthread_cleanup_pop(0);
41
42         // 执行任务,并且在此期间禁止响应取消请求
43         pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
44         (p->task)(p->arg);
45         pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
46
47         free(p);
48     }
49
50     pthread_exit(NULL);
51 }
52 // 函数用于初始化线程池,包括初始化互斥锁、条件变量,分配任务队列和线程数组,并创建指定数量的线程
53 bool init_pool(thread_pool *pool, unsigned int threads_number)
54 {
55     pthread_mutex_init(&pool->lock, NULL);
56     pthread_cond_init(&pool->cond, NULL);
57
58     pool->shutdown = false; // 关闭销毁线程池标识
59     pool->task_list = malloc(sizeof(struct task)); // 任务队列头结点
60     pool->tids = malloc(sizeof(pthread_t) * MAX_ACTIVE_THREADS);
61
62     if(pool->task_list == NULL || pool->tids == NULL)
63     {
64         perror("allocate memory error");
65         return false;
66     }
67
68     pool->task_list->next = NULL;
69
70     pool->waiting_tasks = 0;
71     pool->active_threads = threads_number;
72
73     int i;
74     for(i=0; i<pool->active_threads; i++) // 创建指定数目线程
75     {
76         if(pthread_create(&((pool->tids)[i]), NULL,
77             routine, (void *)pool) != 0)
78         {
79             perror("create threads error");
80             return false;
81         }
82     }
83
84     return true;
85 }
86 // 函数用于向线程池中添加任务,将任务添加到任务队列中,并唤醒一个等待中的线程来执行任务
87 bool add_task(thread_pool *pool, 88 void *(*task)(void *arg), void *arg)
89 {
90     struct task *new_task = malloc(sizeof(struct task)); // 新任务节点
91     if(new_task == NULL)
92     {
93         perror("allocate memory error");
94         return false;
95     }
96     new_task->task = task;
97     new_task->arg = arg;
98     new_task->next = NULL;
99
100     // 访问任务队列前获取互斥锁,此处无需注册取消处理例程
101     pthread_mutex_lock(&pool->lock);
102     if(pool->waiting_tasks >= MAX_WAITING_TASKS)
103     {
104         pthread_mutex_unlock(&pool->lock);
105
106         fprintf(stderr, "too many tasks.\n");
107         free(new_task);
108
109         return false;
110     }
111
112     struct task *tmp = pool->task_list;
113     while(tmp->next != NULL)
114         tmp = tmp->next;
115
116     tmp->next = new_task; // 添加新的任务节点
117     pool->waiting_tasks++;
118
119     // 释放互斥锁,并唤醒其中一个阻塞在条件变量上的线程
120     pthread_mutex_unlock(&pool->lock);
121     pthread_cond_signal(&pool->cond);
122
123     return true;
124 }
125 // 函数用于动态增加线程,根据传入的参数增加指定数量的线程
126 int add_thread(thread_pool *pool, unsigned additional_threads)
127 {
128     if(additional_threads == 0)
129         return 0;
130
131     unsigned total_threads =
132         pool->active_threads + additional_threads;
133
134     int i, actual_increment = 0;
135     for(i = pool->active_threads; // 循环地创建若干指定数目的线程
136         i < total_threads && i < MAX_ACTIVE_THREADS;
137             i++)
138     {
139         if(pthread_create(&((pool->tids)[i]), 140 NULL, routine, (void *)pool) != 0)
141         {
142             perror("add threads error");
143
144             if(actual_increment == 0)
145                 return -1;
146
147             break;
148         }
149         actual_increment++;
150     }
151
152     pool->active_threads += actual_increment;
153     return actual_increment;
154 }
155 // 函数用于动态减少线程,根据传入的参数取消掉指定数量的线程
156 int remove_thread(thread_pool *pool, unsigned int removing_threads)
157 {
158     if(removing_threads == 0)
159         return pool->active_threads;
160
161     int remain_threads = pool->active_threads - removing_threads;
162     remain_threads = remain_threads > 0 ? remain_threads : 1;
163
164     int i; // 循环地取消掉指定数目的线程
165     for(i=pool->active_threads-1; i>remain_threads-1; i--)
166     {
167         errno = pthread_cancel(pool->tids[i]);
168         if(errno != 0)
169             break;
170     }
171
172     if(i == pool->active_threads-1)
173         return -1;
174     else
175     {
176         pool->active_threads = i+1;
177         return i+1;
178     }
179 }
180 // 函数用于销毁线程池,将线程池的shutdown标志设为true,然后广播条件变量以唤醒所有等待中的线程,最后等待所有线程退出并释放资源
181 bool destroy_pool(thread_pool *pool)
182 {
183
184     pool->shutdown = true;
185     pthread_cond_broadcast(&pool->cond);
186
187     int i;
188     for(i=0; i<pool->active_threads; i++)
189     {
190         errno = pthread_join(pool->tids[i], NULL);
191         if(errno != 0)
192         {
193             printf("join tids[%d] error: %s\n", 194 i, strerror(errno));
195         }
196         else
197             printf("[%u] is joined\n", (unsigned)pool->tids[i]);
198
199     }
200
201     free(pool->task_list);
202     free(pool->tids);
203     free(pool);
204
205     return true;
206 }

test.c  测试程序

1 #include "thread_pool.h" 
2 // 模拟了一个需要执行一段时间的任务。任务接受一个参数n,表示任务执行的时间(秒)
3 void *mytask(void *arg)
4 {
5     int n = (int)arg;
6
7     printf("[%u][%s] ==> job will be done in %d sec...\n", 
8         (unsigned)pthread_self(), __FUNCTION__, n);
9
10     sleep(n);
11
12     printf("[%u][%s] ==> job done!\n", 13 (unsigned)pthread_self(), __FUNCTION__);
14
15     return NULL;
16 }
17 // 函数是一个辅助函数,每隔一秒打印一个计时器,用来帮助观察任务执行情况
18 void *count_time(void *arg)
19 {
20     int i = 0;
21     while(1)
22     {
23         sleep(1);
24         printf("sec: %d\n", ++i);
25     }
26 }
27 
28 int main(void)
29 {
30     pthread_t a;
31     pthread_create(&a, NULL, count_time, NULL);// 创建一个辅助线程a,用来计时
32
33     // 1, 初始化一个带有 2 条线程的线程池
34     thread_pool *pool = malloc(sizeof(thread_pool));
35     init_pool(pool, 2);
36
37     // 2, 投入 3 个任务 每个任务执行时间为0到9秒不等
38     printf("throwing 3 tasks...\n");
39     add_task(pool, mytask, (void *)(rand()%10));
40     add_task(pool, mytask, (void *)(rand()%10));
41     add_task(pool, mytask, (void *)(rand()%10));
42
43     // 3, 显示当前有多少条线程
44     printf("current thread number: %d\n", 45 remove_thread(pool, 0));
46     sleep(9);
47
48     // 4, 再投入 2 个任务
49     printf("throwing another 2 tasks...\n");
50     add_task(pool, mytask, (void *)(rand()%10));
51     add_task(pool, mytask, (void *)(rand()%10));
52
53     // 5, 增加 2 条线程
54     add_thread(pool, 2);
55
56     sleep(5);
57
58     // 6, 删除 3 条线程
59     printf("remove 3 threads from the pool, " 
60         "current thread number: %d\n", 61 remove_thread(pool, 3));
62
63     // 7, 销毁线程池
64     destroy_pool(pool);
65     return 0;
66 }

需将上述测试代码中的 mytask 函数改成你需要实现的功能函数即可

  • 5
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码农小白

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值