Qt多线程-QRunnable
QRunnable
#include <QCoreApplication>
#include <QDebug>
#include <QRunnable>
#include <QThreadPool>
#include <QTimer>
class Task : public QRunnable
{
void run()
{
qDebug() << QString("Task, thread id: %1")
.arg(QThread::currentThreadId());
}
};
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
qDebug() << QString("Main, thread id: %1")
.arg(QThread::currentThreadId());
Task *task = new Task;
qDebug() << QString("auto delete flag: %1")
.arg(task->autoDelete());
QThreadPool::globalInstance()->start(task);
QTimer::singleShot(1000, qApp, SLOT(quit()));
return a.exec();
}