Qt 线程池

Qt线程池学习,使用QThreadPool 和QRunnable类

QThreadPool 管理及重复利用线程集合,已减少系统开销。

每一个Qt应用程序都有一个全局的QThreadPool对象,可以直接通过调用globalInstance()进行访问。

QRunnable类是一个接口, 用于表示需要执行的任务或代码段, 具体任务在run() 函数内部实现。

使用QThreadPool里面的线程:

  1. 继承QRunnable并且重写run()函数,实现自己的任务代码。
  2. 创建一个继承QRunnable类的对象。
  3. 通过globalInstance()获取应用程序的全局QThreadPool对象。
  4. 通过使用QThreadPool::start()进行调用。
  5. 在合适的地方调用QThreadPool的waitForDone函数等待所有线程结束。

int  activeThreadCount() const

返回激活线程数量,有可能大于maxThreadCount()值

void  clear()

清空队列等待运行的QRunnable

int expiryTimeout() const

获取超时时间

void setExpiryTimeout(int expiryTimeout)

设置超时时间,及空闲线程到期死亡时间,线程在一定时间内没有被使用,将会死亡。

int maxThreadCount() const

获取最大线程数

void setMaxThreadCount(int maxThreadCount)

设置最大线程数

void start(QRunnable *runnable, int priority = 0)

执行一个任务QRunnable,同时可以设置这个任务的优先级,若线程池有空闲线程,立即执行,否则进入优先级队列等待执

bool tryStart(QRunnable *runnable)

线程池有可用线程时,立即执行任务runnable,返回true,否在立即返回false,不用进入队列等待有空闲线程执行。

bool tryTake(QRunnable *runnable)

在线程池优先级队列中删除任务runnable,返回true,如果队列没有该runnable,返回false

bool waitForDone(int msecs = -1)

等待所以线程执行结束,返回true,msecs 为超时时间,若为-1,则死等全部线程执行结束

void releaseThread()

释放以前通过调用reserveThread()预约的线程。

void reserveThread()

预约一个线程,这个函数会增加活动线程的数量。意味着activeThreadCount()可以返回一个大于maxThreadCount()的值。

void setStackSize(uint stackSize)

设置线程运行的栈大小,默认为0,意味着使用系统默认的栈大小

uint stackSize() const

获取线程运行的栈大小

一个简单的Qt线程池例子

ThreadTask继承QRunnable的类,QRunnable为虚基类,需要重写run函数

threadtask.h  单个任务类声明文件

#ifndef THREADTASK_H
#define THREADTASK_H

#include <QRunnable>

class ThreadTask : public QRunnable
{
public:
    ThreadTask(int ID);
    ~ThreadTask();

    void run();

private:

};

#endif // THREADTASK_H

threadtask.cpp  单个任务类定义文件 

#include "threadtask.h"
#include <QDebug>
#include <QThread>
ThreadTask::ThreadTask(int ID)
{
    setAutoDelete(true);
}

ThreadTask::~ThreadTask()
{

}

void ThreadTask::run()
{
    qDebug()<<"begin thread ID: "<<QThread::currentThreadId()<<endl;
    QThread::sleep(2);
    qDebug()<<"finish thread ID: "<<QThread::currentThreadId()<<endl;
}

threadpool.h  线程池类声明文件

#ifndef THREADPOOL_H
#define THREADPOOL_H

#include<QThreadPool>

class ThreadPool : public QThreadPool
{
public:
    ThreadPool(int MaxThreadCount=5);
    ~ThreadPool();
};

#endif // THREADPOOL_H

threadpool.cpp  线程池定义文件

#include "threadpool.h"

ThreadPool::ThreadPool(int MaxThreadCount)
{
    setMaxThreadCount(MaxThreadCount);
}

ThreadPool::~ThreadPool()
{

}

main.cpp文件

#include<QDebug>
#include<QThread>
#include "threadpool.h"
#include "threadtask.h"

int main()
{
    qDebug()<<"begin: "<<QThread::currentThreadId()<<endl;

    ThreadPool tp;

    for(int i=0;i<30;i++)
    {

        tp.start(new ThreadTask(i));

    }

    tp.waitForDone();

    qDebug()<<"finish"<<endl;
    return 0;
}



运行结果:

begin:  0x26a0

begin thread ID:  0x2acc

begin thread ID:  0xc10

begin thread ID:  0x2318

begin thread ID:  0x24e4

begin thread ID:  0x166c

finish thread ID:  0x166c

finish thread ID:  0x24e4

finish thread ID:  0x2318

finish thread ID:  0xc10

finish thread ID:  0x2acc

begin thread ID:  0x166c

begin thread ID:  0x24e4

begin thread ID:  0x2318

begin thread ID:  0xc10

begin thread ID:  0x2acc

finish thread ID:  0x24e4

finish thread ID:  0xc10

finish thread ID:  0x2318

finish thread ID:  0x2acc

finish thread ID:  0x166c

begin thread ID:  0x24e4

begin thread ID:  0xc10

begin thread ID:  0x2318

begin thread ID:  0x2acc

begin thread ID:  0x166c

finish thread ID:  0x166c

finish thread ID:  0x2318

finish thread ID:  0x2acc

finish thread ID:  0xc10

finish thread ID:  0x24e4

begin thread ID:  0x166c

begin thread ID:  0x2318

begin thread ID:  0x2acc

begin thread ID:  0xc10

begin thread ID:  0x24e4

finish thread ID:  0xc10

finish thread ID:  0x2acc

finish thread ID:  0x166c

begin thread ID:  0xc10

begin thread ID:  0x2acc

begin thread ID:  0x166c

finish thread ID:  0x2318

finish thread ID:  0x24e4

begin thread ID:  0x2318

begin thread ID:  0x24e4

finish thread ID:  0xc10

finish thread ID:  0x2acc

begin thread ID:  0xc10

finish thread ID:  0x166c

begin thread ID:  0x2acc

begin thread ID:  0x166c

finish thread ID:  0x2318

finish thread ID:  0x24e4

begin thread ID:  0x2318

begin thread ID:  0x24e4

finish thread ID:  0x2acc

finish thread ID:  0x166c

finish thread ID:  0xc10

finish thread ID:  0x24e4

finish thread ID:  0x2318

finish

 

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Qt 线程池是一种用于管理多线程任务的工具。它可以在程序中创建一组线程,并将任务分配给这些线程,以实现并发执行任务的效果,从而提高程序的性能和响应速度。 使用 Qt 线程池,需要先创建一个 QThreadPool 对象,并设置最大线程数。然后,将任务封装成 QRunnable 对象,并通过 QThreadPool::start() 函数将任务提交给线程池线程池会自动管理线程的创建和销毁,以及任务的调度和执行。 下面是一个简单的示例代码,演示了如何使用 Qt 线程池: ```c++ #include <QCoreApplication> #include <QThreadPool> #include <QDebug> class MyTask : public QRunnable { public: MyTask(int id) : m_id(id) {} void run() override { qDebug() << "Task" << m_id << "is running on" << QThread::currentThread(); } private: int m_id; }; int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); QThreadPool threadPool; threadPool.setMaxThreadCount(4); for (int i = 0; i < 10; i++) { MyTask *task = new MyTask(i); threadPool.start(task); } threadPool.waitForDone(); qDebug() << "All tasks are done."; return a.exec(); } ``` 在这个示例中,我们创建了一个包含 4 个线程线程池,然后提交了 10 个任务,每个任务都会打印自己的编号和运行线程的编号。最后,我们调用了 QThreadPool::waitForDone() 函数,等待所有任务完成,并输出 "All tasks are done." 消息。 需要注意的是,Qt 线程池只适用于短时间执行的任务,不适合长时间执行的任务。长时间执行的任务需要使用 QThread 或者自定义线程,以避免阻塞线程池
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值