使用公共线程池的例子(QtSDK helloworld )

来源:互联网 发布:靠谱的淘宝优惠券软件 编辑:程序博客网 时间:2024/04/30 02:14
#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;
}