• 【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:

    protected:

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

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

    二、任务子类

    作用

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

    头文件
    成员属性\函数

    public:

    • ​ 默认构造函数

    • ​ 带参构造函数

    • ​ 析构函数

    • ​ Run函数

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

      继承基类的默认构造

    2. 带参构造函数

      继承基类的带参构造

      CSonTask::CSonTask(char* taskname) : CBaseTask(taskname)
      {
      }
      
      • 1
      • 2
      • 3
    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;
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
    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
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267
    • 268
    • 269
    • 270
    • 271
    • 272
    • 273
    • 274
    • 275
    • 276
    • 277
    • 278
    • 279
    • 280
    • 281
    • 282
    • 283
    • 284
    • 285
    • 286
    • 287
    • 288
    • 289
    • 290
    • 291
    • 292
    • 293
    • 294
    • 295
    • 296
    • 297
    • 298
    • 299
    • 300
    • 301
    • 302
    • 303
    • 304
    • 305
    • 306
    • 307
    • 308
    • 309
    • 310
    • 311
    • 312
    • 313
    • 314
    • 315
    • 316
    • 317
    • 318
    • 319
    • 320
    • 321
    • 322
    • 323
    • 324
    • 325
    • 326
    • 327
    • 328
    • 329
    • 330
    • 331
    • 332
    • 333
    • 334
    • 335
    • 336
    • 337
    • 338
    • 339
    • 340
    • 341
    • 342
    • 343
    • 344
    • 345
    • 346
    • 347
    • 348
    • 349
    • 350
    • 351
    • 352
    • 353
    • 354
    • 355
    • 356
    • 357
    • 358
    • 359
    • 360
    • 361
    • 362
    • 363
    • 364
    • 365
    • 366
    • 367
    • 368
    • 369
    • 370
    • 371
    • 372
    • 373
    • 374
    • 375
    • 376
    • 377
    • 378
    • 379
    • 380
    • 381
    • 382
    • 383
    • 384
    • 385
    • 386
    • 387
    • 388
    • 389
    • 390
    • 391
    • 392
    • 393
    • 394
    • 395
    • 396
    • 397
    • 398
    • 399
    • 400
    • 401
    • 402
    • 403
    • 404
    • 405
    • 406
    • 407
    • 408
    • 409
    • 410
    • 411
    • 412
    • 413
    • 414
    • 415
    • 416
    • 417
    • 418
    • 419
    • 420
    • 421
    • 422
    • 423
    • 424
    • 425
    • 426
    • 427
    • 428
    • 429
    • 430
    • 431
    • 432
    • 433
    • 434
    • 435
    • 436
    • 437
    • 438
    • 439
    • 440
    • 441
    • 442
    • 443
    • 444
    • 445
    • 446
    • 447
    • 448
    • 449
    • 450
    • 451
    • 452
    • 453
    • 454
    • 455
    • 456
    • 457
    • 458
    • 459
    • 460
    • 461
    • 462
    • 463
    • 464
    • 465
    • 466
    • 467
    • 468
    • 469
    • 470
    • 471
    • 472
    • 473
    • 474
    • 475
    • 476
    • 477
    • 478
    • 479
    • 480
    • 481
    • 482
    • 483
    • 484
    • 485
    • 486
    • 487
    • 488
    • 489
    • 490
    • 491
    • 492
    • 493
    • 494
    • 495
    • 496
    • 497
    • 498
    • 499
    • 500
    • 501
    • 502
    • 503
    • 504
    • 505
    • 506
    • 507
    • 508
    • 509
    • 510
    • 511
    • 512
    • 513
    • 514
    • 515
    • 516
    • 517
    • 518
    • 519
    • 520
    • 521
    • 522
    • 523
    • 524
    • 525

    问题

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

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

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

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

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

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

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

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

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

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

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

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

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

  • 相关阅读:
    # 杂谈偶感 × 基于QFD方法的质量屋构建
    【.net/.net core】发送邮件方法
    Longhorn 的正确使用姿势:如何处理增量 replica 与其中的 snapshot/backup
    Prepared SQL 性能测试
    java中数据类型byte的底层原理透析
    电子制造企业部署WMS仓储管理系统的好处是什么
    内部即时通讯软件,为企业协同办公保驾护航
    【NodeJs篇】数据库的概念和mysql的基本使用
    Redis缓存序列化配置
    1024特别剪辑: 使用Python Turtle 库绘制一棵随机生成的树
  • 原文地址:https://blog.csdn.net/kin_16/article/details/126671034