#include <QCoreApplication>
#include <QThreadPool>
#include <QThread>
#include "task.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QThread::currentThread()->setObjectName("Main Thread");
qInfo() << "Starting work" << QThread::currentThread();
qInfo() << "Max Threads" << QThreadPool::globalInstance()->maxThreadCount();
for(int i = 0; i < 50; i++)
{
Task *task = new Task();
task->setAutoDelete(true);
QThreadPool::globalInstance()->start(task);
}
qInfo() << "Free to do other things" << QThread::currentThread();
return a.exec();
}
#ifndef TASK_H
#define TASK_H
#include <QObject>
#include <QThread>
#include <QDebug>
#include <QRunnable>
class Task : public QObject, public QRunnable
{
Q_OBJECT
public:
explicit Task(QObject *parent = nullptr);
~Task();
signals:
public slots:
void run();
};
#endif // TASK_H
#include "task.h"
Task::Task(QObject *parent) : QObject(parent)
{
qInfo() << "Constructed" << this << "on" << QThread::currentThread();
}
Task::~Task()
{
qInfo() << "Deconstructed" << this << "on" << QThread::currentThread();
}
void Task::run()
{
QThread *thread = QThread::currentThread();
qInfo() << "Starting" << thread;
for(int i = 0; i < 10; i++)
{
qInfo() << i << "on" << thread;
}
qInfo() << "Finished" << thread;
}