创建一个类继承自QRunnable:
class Thread02 : public QRunnable
重写run方法:
void run() override;
在main函数里面加入线程池:
Thread02 *th = new Thread02();
QThreadPool::globalInstance()->start(th);
#include <QtCore/QCoreApplication>
#include <iostream>
#include "Thread01.h"
#include "Thread02.h"
#include <QThreadPool>
using namespace std;
int main(int argc, char *argv[])
{
? ? QCoreApplication a(argc, argv);
? ? cout << "main thread" << QThread::currentThreadId() << endl;
? ? /*Thread01 th;
? ? th.start();*/
? ? Thread02 *th = new Thread02();
? ? QThreadPool::globalInstance()->start(th);
? ? cout << "main thread end" << endl;
? ? return a.exec();
}