Qt线程池学习,使用QThreadPool 和QRunnable类
QThreadPool 管理及重复利用线程集合,已减少系统开销。
每一个Qt应用程序都有一个全局的QThreadPool对象,可以直接通过调用globalInstance()进行访问。
QRunnable类是一个接口, 用于表示需要执行的任务或代码段, 具体任务在run() 函数内部实现。
使用QThreadPool里面的线程:
- 继承QRunnable并且重写run()函数,实现自己的任务代码。
- 创建一个继承QRunnable类的对象。
- 通过globalInstance()获取应用程序的全局QThreadPool对象。
- 通过使用QThreadPool::start()进行调用。
- 在合适的地方调用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