【C++】线程池(有点乱待补充)

一、基本概念

线程池的实现思想:“管理一个任务队列,一个线程队列,然后每次取一个任务分配给一个线程去做,循环往复。”

因为程序边运行边创建线程是比较耗时的,所以我们通过池化的思想:在程序开始运行前创建多个线程,这样,程序在运行时,只需要从线程池中拿来用就可以了.大大提高了程序运行效率.

线程池是预先创建线程的一种技术。线程池在任务还没有到来之前,创建一定数量(N)的线程,放入空闲队列中。这些线程都是处于阻塞(Suspended)状态,不消耗CPU,但占用较小的内存空间。
当新任务到来时,缓冲池选择一个空闲线程,把任务传入此线程中运行;如果缓冲池已经没有空闲线程,则新建若干个线程。当系统比较空闲时,大部分线程都一直处于暂停状态,线程池自动销毁一部分线程,回收系统资源。

二、特点

1、里面的线程个数有限

2、线程不是只给一个客户端服务,而是所有客户端

3、提前开启一定数量的线程,有客户端就开线程,没客户端就返回到线程池等待,不会频繁开启关闭。

4、当待处理业务过多,空闲队列里的线程都到忙碌线程处理业务,这时候又来了一个业务需要处理,那么此时新增一个线程去忙碌队列处理这个业务。

当任务队列没有任务,但空闲队列里的线程数量比最小数量多,则销毁多余的线程。

三、适用场景

前提:线程本身开销与线程执行任务相比不可忽略

  • 单位时间内处理任务频繁而且任务处理时间短;

  • 对实时性要求较高。如果接受到任务后在创建线程,可能满足不了实时要求,因此必须采用线程池进行预创建。

四、好处

  • 线程池能够减少创建的线程个数,减少线程本身带来的开销

五、组成

六、流程

线程池有三个队列,分别为

(1)空闲线程队列(非队列,不先进先出,只是名字,实际为不定长的链表增删改方便

(2)忙碌线程队列(非队列,只是名字,实际为不定长的链表

(3)任务队列队列,先进先出)

要做的任务(登录、聊天),客户端发送的请求。先进先出。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-T9Dy08E9-1662126640567)(image-20220408194646409.png)]

1、思路

1、任务开始前固定数量的线程在空闲线程队列,所有线程处于等待状态。

2、业务进入任务队列,空闲队列里的一个线程被任务唤醒,空闲队列里的这个线程到忙碌队列处理业务

3、成功结束后,该线程从忙碌队列回到空闲队列

2、注意

待处理业务过多,空闲队列里的线程都到忙碌线程处理业务,这时候又来了一个业务需要处理,那么此时新增 一个线程去忙碌队列处理这个业务。

任务队列没有 任务,但空闲队列里的线程数量比最小数量多,则销毁多余的线程。

★代码实现思路

需要创建

  1. 任务基类
  2. 任务子类:具体业务逻辑
  3. 线程池类:线程池的实现类

之前创建线程只要一个函数,现在是要将创建线程弄成一个类呢,如何操作呢?

线程执行函数是一个void类型的指针函数,也作为类的成员函数,但同时也需要作为pthread_create函数的参数,但是!类的普通成员函数是不可以作为参数的。

原因:

所有的指针需要一个地址,但==在类内部没有地址,也就是类定义的时候,没有分配内存空间==;对类的数据成员取地址,得到的是类的数据成员在类内的 相对偏移量

所以,需要将该类函数(线程执行函数)定义为static静态成员函数

类的成员函数不可以作为函数指针,只有静态的函数才可以

C+±–类1-成员函数做参数.md

创建多少个线程?

因为程序开始运行前要创建一定数量固定数量的线程,线程池有个最小线程数量。

可以利用线程最小数量值循环创建线程,再将任务队列当作参数传给线程执行函数。然后将线程逐个添加进空闲队列中。


有业务来了,要唤醒某个线程到忙碌队列去执行这个业务

那么如何唤醒这个线程呢?

我的理解是,一开始就会创建多个线程,这些线程会在等待队列一直等待,不会立刻执行线程执行函数,直到有业务出现,此时用到线程的条件变量

在任务添加函数中,当判断空闲队列有线程时,使用pthread_cond_signal(cond);函数唤醒这个线程,继而开启互斥锁,执行重要的代码(空闲忙碌队列移动、取任务、做任务),然后解锁。


怎么知道哪个线程要从空闲移动到忙碌呢?

使用参数,move的两个函数都要有线程id作为参数传进来。使用迭代器查找到要移动的被唤醒的线程tid后,进行队列和列表间的添加与删除。

3、代码

一、任务基类

作用
头文件
成员属性\函数

public:

  • ​ 默认构造函数

  • ​ 带参构造函数

  • ​ 析构函数

  • ​ setdata函数

  • ​ Run函数

protected:

  • ​ 业务名字
  • ​ 任务具体数据
成员函数
  1. 默认构造函数
  2. 带参构造函数
  3. 析构函数
  4. setdata函数
  5. Run函数

    业务具体处理逻辑,一个纯虚函数由子类继承做各类业务

二、任务子类

作用

真正处理业务的类,继承基类

头文件
成员属性\函数

public:

  • ​ 默认构造函数

  • ​ 带参构造函数

  • ​ 析构函数

  • ​ Run函数

成员函数
  1. 默认构造函数

    继承基类的默认构造

  2. 带参构造函数

    继承基类的带参构造

    CSonTask::CSonTask(char* taskname) : CBaseTask(taskname)
    {
    }
    
  3. 析构函数
  4. Run函数

    暂无具体与客户端连接业务,先打印

    模拟时间损耗sleep

三、线程池类

作用
头文件
成员属性\函数

public:

  • ​ 带参构造函数

  • 参数:要创建的最小线程数量

    将线程最大数量宏定义

  • ​ 析构函数

  • ​ 停止所有线程函数

  • ​ 创建线程函数

  • ​ 添加任务到任务队列函数

protected:

  • ​ (静态)线程执行函数
  • ​ (静态)将线程从忙碌队列移动到空闲队列函数
  • ​ (静态)将线程从空闲队列移动到忙碌队列函数

private:

  • ​ 线程最小数量

  • ​ 线程最大数量

  • ​ 任务队列(queue)

  • 1、类型应该是任务类类型,因为任务队列一般是有很多个任务,用任务子类(CSonTask)类型只能是一个,所以类型应该是任务基类(CBaseTask)类型的,且要是指针,这样可以用父类指针指向子类空间

    以上利用了面向对象的多态特性

    多态可以让同名函数,因为函数指向对象的不同,而去调用该对象中该名称的函数

    2、该成员变量不需要设置为静态的

  • ​ (静态)线程条件变量

  • 决定线程的等待和唤醒

  • ​ (静态)空闲队列(list)

  • ​ (静态)忙碌队列(list)

  • ​ (静态)三个队列互斥量

  • 注意:上述类成员变量之所以是静态的是因为,线程执行函数是静态函数,静态函数只能访问静态变量

成员函数

记得加命名空间

首先进行静态成员初始化

注意互斥量初始化要用宏定义

  1. 带参构造函数

    线程数量值初始化

  2. 线程池创建函数

    目的:创建线程

    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;
    }
    
  3. 线程池关闭函数
  1. 任务添加函数

    1、任务队列添加传进来的任务

    2、判断空闲队列是否有线程

    有的话,唤醒线程(条件变量)

    没有线程

  2. 线程执行函数

    注意:需要是静态 的,并且只能让类成员访问,所以是protected***的。

    1、获取线程自己的tid

    2、将传进来的参数(任务)赋值给任务队列

    3、死循环

    ​ (1)让线程等待,用到条件变量的函数,等待之前先加锁

    ​ (2)将该线程移到忙碌队列

    ​ (3)从任务队列取任务

    头部取值,任务

    ​ (4)从任务队列删除该任务,不然其他线程会来抢,解锁

    ​ (5)执行任务run

    ​ (6)移到空闲队列

    ​ (7)判断该线程做完一个任务后三种情况对应措施

    ​ 3.7.1 还有任务、空闲队列还有线程

    ​ 3.7.2 还有任务,没有空闲线程

    ​ 3.7.3 没有任务,还有空闲线程(数量大于最少线程数量)

  3. 空闲移动到忙碌
  4. 忙碌添加到空闲

    1、创建迭代器

    2、从空闲队列遍历查找要移动的线程id

    3、判断是否查找到tid(不等于链表末尾)

    4、找到后

    • 从空闲移除

    加锁

    list容器移除–erase(iter)

    解锁

    • 添加到忙碌队列

    加锁

    添加–push_back(tid)

    解锁

main测试函数

1、创建线程池对象

2、循环创建任务,将任务数据添加

3、当前线程池添加任务唤醒线程

bug

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-fQzYHzWR-1662126640569)(image-20220410004440354.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 

问题

1、为什么要使用线程池?

T1:线程创建时间
T2:线程执行时间,包括线程的同步等时间
T3:线程销毁时间

传统的创建多个线程需要多次进行上面三个步骤,不断的创建、执行和销毁,开销较大。

而线程池开始做一次创建多个线程,最后一次关闭所有线程,中间不断地进行线程执行操作。也就是一次T1、一次T3,多次T2。减少了不断创建销毁线程的这部分开销。

2、线程什么时候增加和减少呢?

什么时候开始从最小数量开始增减,反之

当待处理业务过多,空闲队列里的线程都到忙碌线程处理业务,这时候又来了一个业务需要处理,那么此时新增一个线程去忙碌队列处理这个业务。

当任务队列没有任务,但空闲队列里的线程数量比最小数量多,则销毁多余的线程。

3、怎样解决队列不安全(线程同步)的问题?

一个任务可能有多个线程想去处理,多个线程想进行相同的操作,造成数据不安全。

线程同步问题–加互斥锁解决

4、怎么知道哪个线程要从空闲移动到忙碌呢?

参数,move的两个函数都要有线程id作为参数传进来

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值