QT 线程池的使用

#include <QCoreApplication>

#include <QThreadPool>
#include <QRunnable>
#include <QDebug>

class MyTask : public QRunnable
{
public:
    void run() override
    {
        // 在这里执行你的任务
        qDebug() << "Task is running on thread" << QThread::currentThreadId();
        QThread::sleep(2);//等2秒,看得清楚点
    }
};

int main(int argc, char *argv[])
{
    // 初始化应用程序
    QCoreApplication app(argc, argv);

    // 设置线程池的最大线程数
    QThreadPool::globalInstance()->setMaxThreadCount(4);

    // 创建并启动任务
    for (int i = 0; i < 10; ++i)
    {
        MyTask *task = new MyTask();
        QThreadPool::globalInstance()->start(task);
    }

    return app.exec();
}
 

相关推荐

  1. QT 线使用

    2024-03-28 17:16:03       22 阅读
  2. Qt线

    2024-03-28 17:16:03       35 阅读
  3. 线使用

    2024-03-28 17:16:03       15 阅读
  4. Qt提高-线QThreadPool 详解

    2024-03-28 17:16:03       25 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-28 17:16:03       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-28 17:16:03       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-28 17:16:03       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-28 17:16:03       20 阅读

热门阅读

  1. php 快速入门(六)

    2024-03-28 17:16:03       19 阅读
  2. kafka

    kafka

    2024-03-28 17:16:03      16 阅读
  3. ffmpeg命令行

    2024-03-28 17:16:03       20 阅读
  4. Leetcode 169

    2024-03-28 17:16:03       21 阅读
  5. go加载配置

    2024-03-28 17:16:03       17 阅读
  6. python快速入门一

    2024-03-28 17:16:03       19 阅读
  7. Http和Https

    2024-03-28 17:16:03       20 阅读