#include <QtCore>
// A hello world program to demonstrate the use of the global thread pool
// hellothreadpool/main.cpp
class Work : public QRunnable
{
public:
void run()
{
qDebug() << "Hello from thread " << QThread::currentThread();
}
};
int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);
Work work;
work.setAutoDelete(false);
QThreadPool *threadPool = QThreadPool::globalInstance(); //使用公共线程池
threadPool->start(&work);
qDebug() << "hello from GUI thread " << QThread::currentThread();
threadPool->waitForDone();
return 0;
}
// A hello world program to demonstrate the use of the global thread pool
// hellothreadpool/main.cpp
class Work : public QRunnable
{
public:
void run()
{
qDebug() << "Hello from thread " << QThread::currentThread();
}
};
int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);
Work work;
work.setAutoDelete(false);
QThreadPool *threadPool = QThreadPool::globalInstance(); //使用公共线程池
threadPool->start(&work);
qDebug() << "hello from GUI thread " << QThread::currentThread();
threadPool->waitForDone();
return 0;
}
本文通过一个简单的Qt程序示例介绍了如何使用Qt的全局线程池来执行任务。该示例创建了一个继承自QRunnable的Work类,并在主线程中启动了全局线程池来运行Work实例的任务。
309

被折叠的 条评论
为什么被折叠?



