Qt线程池

2023-12-17 14:39:13

创建一个类继承自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();
}

文章来源:https://blog.csdn.net/weixin_38241876/article/details/135044175
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。