#include <QThreadPool>
#include <QRunnable>
#include <QTimer>
class Worker : public QRunnable {
public:
void run() override {
// 在这里执行耗时任务
qDebug() << "Worker is running";
}
};
int main(int argc, char **argv) {
QCoreApplication app(argc, argv);
QThreadPool* pool = QThreadPool::globalInstance();
int numTasks = 10;
for (int i = 0; i < numTasks; ++i) {
Worker* worker = new Worker();
pool->start(worker);
}
// 等待所有任务完成
while (!pool->isEmpty()) {
QThread::msleep(100);
}
return app.exec();
}
08-14
1548
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
08-09
539
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
06-02