## 线 程 池
#### 一、介绍线程池的文件
```c
thread_pool.c --> 线程池源码,用户不需要修改,如果要使用线程池接口,只需要把这个文件一起编译 就行。
thread_pool.h --> 线程池中结构体,函数声明,宏定义..
main.c --> 自己写的案例,里面就是调用了线程池接口,用户只需要根据不同的需求来修改这个文件即可。
```
#### 二、线程池实现过程。
##### 1、什么是线程池?
```c
线程池就是多个线程组合在一起的集合,就像一家公司一样,由多个员工组成的一个集合,当有任务时, 这些线程就会去处理任务,当没有任务时,线程就会休息。
```
##### 2、如何描述一个线程池? -> 使用一个结构体来描述。
```c
typedef struct thread_pool
{
pthread_mutex_t lock; ---> 互斥锁
pthread_cond_t cond; ---> 条件变量
bool shutdown; ---> 线程池状态 true->关闭 false->开启
struct task *task_list; ---> 任务队列头节点
pthread_t *tids; ---> 存储线程池中所有线程ID号空间的地址
unsigned max_waiting_tasks; ---> 线程池中最大等待的任务个数
unsigned waiting_tasks; ---> 线程池正在等待的任务个数
unsigned active_threads; ---> 当前线程池线程的个数
}thread_pool;
给struct thread_pool取了一个新的别名叫thread_pool。
不取别名,声明结构体变量和指针:
struct thread_pool pool;
struct thread_pool *pool;
取了别名之后,声明:
thread_pool pool;
thread_pool *pool;
//任务链表结构体的原型
struct task
{
void *(*do_task)(void *arg); ---> 任务执行函数
void *arg; ---> 参数
struct task *next; ---> 指针域,用于存放下一个任务的地址
};
```
#### 三、线程池实现接口。
##### 1、初始化线程池。
```c