线程池的实现思想:“管理一个任务队列,一个线程队列,然后每次取一个任务分配给一个线程去做,循环往复。”
因为程序边运行边创建线程是比较耗时的,所以我们通过池化的思想:在程序开始运行前创建多个线程,这样,程序在运行时,只需要从线程池中拿来用就可以了.大大提高了程序运行效率.
线程池是预先创建线程的一种技术。线程池在任务还没有到来之前,创建一定数量(N)的线程,放入空闲队列中。这些线程都是处于阻塞(Suspended)状态,不消耗CPU,但占用较小的内存空间。
当新任务到来时,缓冲池选择一个空闲线程,把任务传入此线程中运行;如果缓冲池已经没有空闲线程,则新建若干个线程。当系统比较空闲时,大部分线程都一直处于暂停状态,线程池自动销毁一部分线程,回收系统资源。
1、里面的线程个数有限的
2、线程不是只给一个客户端服务,而是所有客户端
3、提前开启一定数量的线程,有客户端就开线程,没客户端就返回到线程池等待,不会频繁开启关闭。
4、当待处理业务过多,空闲队列里的线程都到忙碌线程处理业务,这时候又来了一个业务需要处理,那么此时新增一个线程去忙碌队列处理这个业务。
当任务队列没有任务,但空闲队列里的线程数量比最小数量多,则销毁多余的线程。
前提:线程本身开销与线程执行任务相比不可忽略
单位时间内处理任务频繁而且任务处理时间短;
对实时性要求较高。如果接受到任务后在创建线程,可能满足不了实时要求,因此必须采用线程池进行预创建。
线程池有三个队列,分别为
(1)空闲线程队列(非队列,不先进先出,只是名字,实际为不定长的链表,增删改方便)
(2)忙碌线程队列(非队列,只是名字,实际为不定长的链表)
(3)任务队列(队列,先进先出)
要做的任务(登录、聊天),客户端发送的请求。先进先出。
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-T9Dy08E9-1662126640567)(image-20220408194646409.png)]
1、任务开始前有固定数量的线程在空闲线程队列,所有线程处于等待状态。
2、业务进入任务队列,空闲队列里的一个线程被任务唤醒,空闲队列里的这个线程到忙碌队列处理业务
3、成功结束后,该线程从忙碌队列回到空闲队列
当待处理业务过多,空闲队列里的线程都到忙碌线程处理业务,这时候又来了一个业务需要处理,那么此时新增 一个线程去忙碌队列处理这个业务。
当任务队列没有 任务,但空闲队列里的线程数量比最小数量多,则销毁多余的线程。
线程执行函数是一个void类型的指针函数,也作为类的成员函数,但同时也需要作为pthread_create函数的参数,但是!类的普通成员函数是不可以作为参数的。
原因:
所有的指针需要一个地址,但==在类内部没有地址,也就是类定义的时候,没有分配内存空间==;对类的数据成员取地址,得到的是类的数据成员在类内的 相对偏移量 ;
所以,需要将该类函数(线程执行函数)定义为static静态成员函数。
类的成员函数不可以作为函数指针,只有静态的函数才可以
因为程序开始运行前要创建一定数量固定数量的线程,线程池有个最小线程数量。
可以利用线程最小数量值循环创建线程,再将任务队列当作参数传给线程执行函数。然后将线程逐个添加进空闲队列中。
有业务来了,要唤醒某个线程到忙碌队列去执行这个业务
我的理解是,一开始就会创建多个线程,这些线程会在等待队列一直等待,不会立刻执行线程执行函数,直到有业务出现,此时用到线程的条件变量。
在任务添加函数中,当判断空闲队列有线程时,使用pthread_cond_signal(cond);函数唤醒这个线程,继而开启互斥锁,执行重要的代码(空闲忙碌队列移动、取任务、做任务),然后解锁。
使用参数,move的两个函数都要有线程id作为参数传进来。使用迭代器查找到要移动的被唤醒的线程tid后,进行队列和列表间的添加与删除。
public:
protected:
真正处理业务的类,继承基类
public:
默认构造函数
带参构造函数
析构函数
Run函数
继承基类的默认构造
继承基类的带参构造
CSonTask::CSonTask(char* taskname) : CBaseTask(taskname)
{
}
暂无具体与客户端连接业务,先打印
模拟时间损耗sleep
public:
带参构造函数
参数:要创建的最小线程数量
将线程最大数量宏定义
析构函数
停止所有线程函数
创建线程函数
添加任务到任务队列函数
protected:
private:
线程最小数量
线程最大数量
任务队列(queue)
1、类型应该是任务类类型,因为任务队列一般是有很多个任务,用任务子类(CSonTask)类型只能是一个,所以类型应该是任务基类(CBaseTask)类型的,且要是指针,这样可以用父类指针指向子类空间
以上利用了面向对象的多态特性:
多态可以让同名函数,因为函数指向对象的不同,而去调用该对象中该名称的函数
2、该成员变量不需要设置为静态的
(静态)线程条件变量
决定线程的等待和唤醒
(静态)空闲队列(list)
(静态)忙碌队列(list)
(静态)三个队列互斥量
注意:上述类成员变量之所以是静态的是因为,线程执行函数是静态函数,静态函数只能访问静态变量。
记得加命名空间
首先进行静态成员初始化
注意互斥量初始化要用宏定义
线程数量值初始化
目的:创建线程
1、利用线程最小数量值循环创建线程,将任务队列当作参数传给线程执行函数。
2、将创建的线程塞进空闲队列
//线程创建,进空闲队列
int CThreadPool::create()
{
for (int i = 0; i < this->threadMinNum; i++)
{
pthread_t tid = 0;
pthread_create(&tid,NULL, pthread_function,&taskQueue);
idleThread.push_back(tid);
}
return 1;
}
1、任务队列添加传进来的任务
2、判断空闲队列是否有线程
有的话,唤醒线程(条件变量)
没有线程
注意:需要是静态 的,并且只能让类成员访问,所以是protected***的。
1、获取线程自己的tid
2、将传进来的参数(任务)赋值给任务队列
3、死循环
(1)让线程等待,用到条件变量的函数,等待之前先加锁
(2)将该线程移到忙碌队列
(3)从任务队列取任务
头部取值,任务
(4)从任务队列删除该任务,不然其他线程会来抢,解锁
(5)执行任务run
(6)移到空闲队列
(7)判断该线程做完一个任务后三种情况对应措施
3.7.1 还有任务、空闲队列还有线程
3.7.2 还有任务,没有空闲线程
3.7.3 没有任务,还有空闲线程(数量大于最少线程数量)
1、创建迭代器
2、从空闲队列遍历查找要移动的线程id
3、判断是否查找到tid(不等于链表末尾)
4、找到后
加锁
list容器移除–erase(iter)
解锁
加锁
添加–push_back(tid)
解锁
1、创建线程池对象
2、循环创建任务,将任务数据添加
3、当前线程池添加任务唤醒线程
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-fQzYHzWR-1662126640569)(image-20220410004440354.png)]](https://1000bd.com/contentImg/2023/10/29/022054915.png)
未加命令行
root@ubuntu:~/projects/ThreadPool1/bin/x64/Debug# ./ThreadPool1.out
CThreadPool
create
create 0
create 1
create 2
create 3
create 4
create 5
create 6
create 7
1该线程tid=140496591025920 run------
pthread_function
pthread_function while lock
pthread_function while pthread_cond_wait s
1该线程tid=140496599418624 run------
pthread_function
pthread_function while lock
pthread_function while pthread_cond_wait s
1该线程tid=140496607811328 run------
pthread_function
pthread_function while lock
pthread_function while pthread_cond_wait s
1该线程tid=140496582633216 run------
pthread_function
pthread_function while lock
pthread_function while pthread_cond_wait s
1该线程tid=140496616204032 run------
pthread_function
pthread_function while lock
pthread_function while pthread_cond_wait s
1该线程tid=140496574240512 run------
pthread_function
pthread_function while lock
pthread_function while pthread_cond_wait s
1该线程tid=140496624596736 run------
pthread_function
pthread_function while lock
pthread_function while pthread_cond_wait s
1该线程tid=140496632989440 run------
pthread_function
pthread_function while lock
pthread_function while pthread_cond_wait s
create 8
create 9
addTask
22该线程tid=140496591025920 run------MoveToBusy
now Taskqueue num= 0
now idlelist num= 9
now busylist num= 1
----------start run-----------
test ThreadPool。。。。。
唤醒:pthread_cond_signal
task1
addTask
22该线程tid=140496599418624 run------MoveToBusy
now Taskqueue num= 0
now idlelist num= 8
now busylist num= 2
----------start run-----------
test ThreadPool。。。。。
唤醒:pthread_cond_signal
task2
addTask
22该线程tid=140496607811328 run------MoveToBusy
now Taskqueue num= 0
now idlelist num= 7
now busylist num= 3
----------start run-----------
test ThreadPool。。。。。
唤醒:pthread_cond_signal
task3
addTask
22该线程tid=140496582633216 run------MoveToBusy
now Taskqueue num= 0
now idlelist num= 6
now busylist num= 4
----------start run-----------
test ThreadPool。。。。。
唤醒:pthread_cond_signal
task4
addTask
22该线程tid=140496616204032 run------MoveToBusy
now Taskqueue num= 0
now idlelist num= 5
now busylist num= 5
----------start run-----------
test ThreadPool。。。。。
唤醒:pthread_cond_signal
task5
addTask
22该线程tid=140496574240512 run------MoveToBusy
now Taskqueue num= 0
now idlelist num= 4
now busylist num= 6
----------start run-----------
test ThreadPool。。。。。
唤醒:pthread_cond_signal
task6
addTask
22该线程tid=140496624596736 run------MoveToBusy
now Taskqueue num= 0
now idlelist num= 3
now busylist num= 7
----------start run-----------
test ThreadPool。。。。。
唤醒:pthread_cond_signal
task7
addTask
22该线程tid=140496632989440 run------MoveToBusy
now Taskqueue num= 0
now idlelist num= 2
now busylist num= 8
----------start run-----------
test ThreadPool。。。。。
唤醒:pthread_cond_signal
task8
addTask
唤醒:pthread_cond_signal
task9
addTask
唤醒:pthread_cond_signal
task10
addTask
唤醒:pthread_cond_signal
task11
addTask
唤醒:pthread_cond_signal
task12
addTask
唤醒:pthread_cond_signal
task13
addTask
唤醒:pthread_cond_signal
task14
addTask
唤醒:pthread_cond_signal
task15
addTask
唤醒:pthread_cond_signal
task16
addTask
唤醒:pthread_cond_signal
task17
addTask
唤醒:pthread_cond_signal
task18
addTask
唤醒:pthread_cond_signal
task19
addTask
唤醒:pthread_cond_signal
task20
addTask
唤醒:pthread_cond_signal
task21
addTask
唤醒:pthread_cond_signal
task22
addTask
唤醒:pthread_cond_signal
task23
addTask
唤醒:pthread_cond_signal
task24
addTask
唤醒:pthread_cond_signal
task25
addTask
唤醒:pthread_cond_signal
task26
addTask
唤醒:pthread_cond_signal
task27
addTask
唤醒:pthread_cond_signal
task28
addTask
唤醒:pthread_cond_signal
task29
addTask
唤醒:pthread_cond_signal
task30
1该线程tid=140496557455104 run------
pthread_function
pthread_function while lock
pthread_function while pthread_cond_wait s
1该线程tid=140496565847808 run------
pthread_function
pthread_function while lock
pthread_function while pthread_cond_wait s
----------run stop-----------
pthread tid 140496591025920is ---idle--- MoveToIdle
MoveToIdle
该线程休息。让其他线程去做剩余的任务 或者 该线程做下个任务
MoveToBusy
pthread_function while lock
----------run stop-----------
pthread tid 140496599418624is ---idle--- MoveToIdle
MoveToIdle
该线程休息。让其他线程去做剩余的任务 或者 该线程做下个任务
MoveToBusy
pthread_function while lock
22该线程tid=140496557455104 run------MoveToBusy
now Taskqueue num= 21
now idlelist num= 1
now busylist num= 9
----------start run-----------
test ThreadPool。。。。。
----------run stop-----------
pthread tid 140496607811328is ---idle--- MoveToIdle
MoveToIdle
该线程休息。让其他线程去做剩余的任务 或者 该线程做下个任务
MoveToBusy
pthread_function while lock
pthread_function while pthread_cond_wait s
22该线程tid=140496565847808 run------MoveToBusy
now Taskqueue num= 20
now idlelist num= 0
now busylist num= 10
----------start run-----------
test ThreadPool。。。。。
pthread_function while pthread_cond_wait s
pthread_function while pthread_cond_wait s
----------run stop-----------
pthread tid 140496582633216is ---idle--- MoveToIdle
MoveToIdle
该线程休息。让其他线程去做剩余的任务 或者 该线程做下个任务
MoveToBusy
pthread_function while lock
----------run stop-----------
pthread tid 140496616204032is ---idle--- MoveToIdle
MoveToIdle
该线程休息。让其他线程去做剩余的任务 或者 该线程做下个任务
MoveToBusy
pthread_function while lock
22该线程tid=140496591025920 run------MoveToBusy
now Taskqueue num= 19
now idlelist num= 0
now busylist num= 10
----------start run-----------
test ThreadPool。。。。。
pthread_function while pthread_cond_wait s
22该线程tid=140496599418624 run------MoveToBusy
now Taskqueue num= 18
now idlelist num= 0
now busylist num= 10
----------start run-----------
test ThreadPool。。。。。
pthread_function while pthread_cond_wait s
----------run stop-----------
pthread tid 140496574240512is ---idle--- MoveToIdle
MoveToIdle
该线程休息。让其他线程去做剩余的任务 或者 该线程做下个任务
MoveToBusy
pthread_function while lock
----------run stop-----------
pthread tid 140496624596736is ---idle--- MoveToIdle
MoveToIdle
该线程休息。让其他线程去做剩余的任务 或者 该线程做下个任务
MoveToBusy
pthread_function while lock
22该线程tid=140496607811328 run------MoveToBusy
now Taskqueue num= 17
now idlelist num= 0
now busylist num= 10
----------start run-----------
test ThreadPool。。。。。
pthread_function while pthread_cond_wait s
22该线程tid=140496582633216 run------MoveToBusy
now Taskqueue num= 16
now idlelist num= 0
now busylist num= 10
----------start run-----------
test ThreadPool。。。。。
pthread_function while pthread_cond_wait s
----------run stop-----------
pthread tid 140496632989440is ---idle--- MoveToIdle
MoveToIdle
该线程休息。让其他线程去做剩余的任务 或者 该线程做下个任务
MoveToBusy
pthread_function while lock
22该线程tid=140496616204032 run------MoveToBusy
now Taskqueue num= 15
now idlelist num= 0
now busylist num= 10
----------start run-----------
test ThreadPool。。。。。
pthread_function while pthread_cond_wait s
----------run stop-----------
pthread tid 140496557455104is ---idle--- MoveToIdle
MoveToIdle
该线程休息。让其他线程去做剩余的任务 或者 该线程做下个任务
MoveToBusy
pthread_function while lock
----------run stop-----------
pthread tid 140496565847808is ---idle--- MoveToIdle
MoveToIdle
该线程休息。让其他线程去做剩余的任务 或者 该线程做下个任务
MoveToBusy
pthread_function while lock
22该线程tid=140496574240512 run------MoveToBusy
now Taskqueue num= 14
now idlelist num= 0
now busylist num= 10
----------start run-----------
test ThreadPool。。。。。
pthread_function while pthread_cond_wait s
22该线程tid=140496624596736 run------MoveToBusy
now Taskqueue num= 13
now idlelist num= 0
now busylist num= 10
----------start run-----------
test ThreadPool。。。。。
----------run stop-----------
pthread tid 140496591025920is ---idle--- MoveToIdle
MoveToIdle
该线程休息。让其他线程去做剩余的任务 或者 该线程做下个任务
MoveToBusy
pthread_function while lock
----------run stop-----------
pthread tid 140496599418624is ---idle--- MoveToIdle
MoveToIdle
该线程休息。让其他线程去做剩余的任务 或者 该线程做下个任务
MoveToBusy
pthread_function while lock
22该线程tid=140496632989440 run------MoveToBusy
now Taskqueue num= 12
now idlelist num= 0
now busylist num= 10
----------start run-----------
test ThreadPool。。。。。
----------run stop-----------
pthread tid 140496607811328is ---idle--- MoveToIdle
MoveToIdle
该线程休息。让其他线程去做剩余的任务 或者 该线程做下个任务
MoveToBusy
pthread_function while lock
pthread_function while pthread_cond_wait s
----------run stop-----------
pthread tid 140496582633216is ---idle--- MoveToIdle
MoveToIdle
该线程休息。让其他线程去做剩余的任务 或者 该线程做下个任务
MoveToBusy
pthread_function while lock
pthread_function while pthread_cond_wait s
pthread_function while pthread_cond_wait s
pthread_function while pthread_cond_wait s
pthread_function while pthread_cond_wait s
22该线程tid=140496591025920 run------MoveToBusy
now Taskqueue num= 11
now idlelist num= 0
now busylist num= 10
----------start run-----------
test ThreadPool。。。。。
22该线程tid=140496557455104 run------MoveToBusy
now Taskqueue num= 10
now idlelist num= 0
now busylist num= 10
----------start run-----------
test ThreadPool。。。。。
----------run stop-----------
pthread tid 140496616204032is ---idle--- MoveToIdle
MoveToIdle
该线程休息。让其他线程去做剩余的任务 或者 该线程做下个任务
MoveToBusy
pthread_function while lock
pthread_function while pthread_cond_wait s
22该线程tid=140496582633216 run------MoveToBusy
now Taskqueue num= 9
now idlelist num= 0
now busylist num= 10
----------start run-----------
test ThreadPool。。。。。
----------run stop-----------
pthread tid 140496574240512is ---idle--- MoveToIdle
MoveToIdle
该线程休息。让其他线程去做剩余的任务 或者 该线程做下个任务
MoveToBusy
pthread_function while lock
----------run stop-----------
pthread tid 140496624596736is ---idle--- MoveToIdle
MoveToIdle
该线程休息。让其他线程去做剩余的任务 或者 该线程做下个任务
MoveToBusy
pthread_function while lock
----------run stop-----------
pthread tid 140496591025920is ---idle--- MoveToIdle
MoveToIdle
该线程休息。让其他线程去做剩余的任务 或者 该线程做下个任务
MoveToBusy
pthread_function while lock
----------run stop-----------
pthread tid 140496582633216is ---idle--- MoveToIdle
MoveToIdle
该线程休息。让其他线程去做剩余的任务 或者 该线程做下个任务
MoveToBusy
pthread_function while lock
----------run stop-----------
pthread tid 140496632989440is ---idle--- MoveToIdle
MoveToIdle
该线程休息。让其他线程去做剩余的任务 或者 该线程做下个任务
MoveToBusy
pthread_function while lock
22该线程tid=140496599418624 run------MoveToBusy
now Taskqueue num= 8
now idlelist num= 0
now busylist num= 10
----------start run-----------
test ThreadPool。。。。。
----------run stop-----------
pthread tid 140496557455104is ---idle--- MoveToIdle
MoveToIdle
该线程休息。让其他线程去做剩余的任务 或者 该线程做下个任务
MoveToBusy
pthread_function while lock
pthread_function while pthread_cond_wait s
pthread_function while pthread_cond_wait s
22该线程tid=140496565847808 run------MoveToBusy
now Taskqueue num= 7
now idlelist num= 0
now busylist num= 10
----------start run-----------
test ThreadPool。。。。。
pthread_function while pthread_cond_wait s
22该线程tid=140496607811328 run------MoveToBusy
now Taskqueue num= 6
now idlelist num= 0
now busylist num= 10
----------start run-----------
test ThreadPool。。。。。
pthread_function while pthread_cond_wait s
22该线程tid=140496616204032 run------MoveToBusy
now Taskqueue num= 5
now idlelist num= 0
now busylist num= 10
----------start run-----------
test ThreadPool。。。。。
pthread_function while pthread_cond_wait s
pthread_function while pthread_cond_wait s
----------run stop-----------
pthread tid 140496599418624is ---idle--- MoveToIdle
MoveToIdle
该线程休息。让其他线程去做剩余的任务 或者 该线程做下个任务
MoveToBusy
pthread_function while lock
22该线程tid=140496557455104 run------MoveToBusy
now Taskqueue num= 4
now idlelist num= 0
now busylist num= 10
----------start run-----------
test ThreadPool。。。。。
pthread_function while pthread_cond_wait s
----------run stop-----------
pthread tid 140496565847808is ---idle--- MoveToIdle
MoveToIdle
该线程休息。让其他线程去做剩余的任务 或者 该线程做下个任务
MoveToBusy
pthread_function while lock
22该线程tid=140496574240512 run------MoveToBusy
now Taskqueue num= 3
now idlelist num= 0
now busylist num= 10
----------start run-----------
test ThreadPool。。。。。
----------run stop-----------
pthread tid 140496616204032is ---idle--- MoveToIdle
MoveToIdle
该线程休息。让其他线程去做剩余的任务 或者 该线程做下个任务
MoveToBusy
pthread_function while lock
----------run stop-----------
pthread tid 140496607811328is ---idle--- MoveToIdle
MoveToIdle
该线程休息。让其他线程去做剩余的任务 或者 该线程做下个任务
MoveToBusy
pthread_function while lock
22该线程tid=140496624596736 run------MoveToBusy
now Taskqueue num= 2
now idlelist num= 0
now busylist num= 10
----------start run-----------
test ThreadPool。。。。。
pthread_function while pthread_cond_wait s
pthread_function while pthread_cond_wait s
22该线程tid=140496591025920 run------MoveToBusy
now Taskqueue num= 1
now idlelist num= 0
now busylist num= 10
----------start run-----------
test ThreadPool。。。。。
pthread_function while pthread_cond_wait s
----------run stop-----------
pthread tid 140496557455104is ---idle--- MoveToIdle
MoveToIdle
该线程休息。让其他线程去做剩余的任务 或者 该线程做下个任务
MoveToBusy
pthread_function while lock
22该线程tid=140496582633216 run------MoveToBusy
now Taskqueue num= 0
now idlelist num= 0
now busylist num= 10
----------start run-----------
test ThreadPool。。。。。
----------run stop-----------
pthread tid 140496574240512is ---idle--- MoveToIdle
MoveToIdle
pthread_function while lock
pthread_function while pthread_cond_wait s
----------run stop-----------
pthread tid 140496624596736is ---idle--- MoveToIdle
MoveToIdle
pthread_function while lock
pthread_function while pthread_cond_wait s
----------run stop-----------
pthread tid 140496591025920is ---idle--- MoveToIdle
MoveToIdle
pthread_function while lock
pthread_function while pthread_cond_wait s
pthread_function while pthread_cond_wait s
----------run stop-----------
pthread tid 140496582633216is ---idle--- MoveToIdle
MoveToIdle
pthread_function while lock
pthread_function while pthread_cond_wait s
T1:线程创建时间
T2:线程执行时间,包括线程的同步等时间
T3:线程销毁时间
传统的创建多个线程需要多次进行上面三个步骤,不断的创建、执行和销毁,开销较大。
而线程池开始做一次创建多个线程,最后一次关闭所有线程,中间不断地进行线程执行操作。也就是一次T1、一次T3,多次T2。减少了不断创建销毁线程的这部分开销。
什么时候开始从最小数量开始增减,反之
当待处理业务过多,空闲队列里的线程都到忙碌线程处理业务,这时候又来了一个业务需要处理,那么此时新增一个线程去忙碌队列处理这个业务。
当任务队列没有任务,但空闲队列里的线程数量比最小数量多,则销毁多余的线程。
一个任务可能有多个线程想去处理,多个线程想进行相同的操作,造成数据不安全。
线程同步问题–加互斥锁解决
参数,move的两个函数都要有线程id作为参数传进来