墨香年少 32 发布于 2023年8月13日 counter.h: #ifndef COUNTER_H #define COUNTER_H #include <QObject> #include <QThread> #include <QDebug> #include <QRandomGenerator> class Counter : public QObject { Q_OBJECT public: explicit Counter(QObject *parent = nullptr); signals: public slots: void start(); }; #endif // COUNTER_H counter.cpp: #include "counter.h" Counter::Counter(QObject *parent) : QObject{parent} { } void Counter::start() { for (int var = 0; var < 20; var++) { qInfo() << "[" << QThread::currentThread()->objectName() << "]" << var; int value = static_cast<unsigned long>(QRandomGenerator::global()->bounded(500)); QThread::currentThread()->msleep(value); } QThread::currentThread()->quit(); } manager.h: #ifndef MANAGER_H #define MANAGER_H #include <QObject> #include <QThread> #include <QDebug> #include "counter.h" class Manager : public QObject { Q_OBJECT public: explicit Manager(QObject *parent = nullptr); void start(); signals: public slots: void started(); void finished(); private: QList<QThread*> threads; }; #endif // MANAGER_H manager.cpp: #include "manager.h" Manager::Manager(QObject *parent) : QObject{parent} { for (int var = 0; var < 5; var++) { QThread* thread = new QThread(this); thread->setObjectName("thread"+QString::number(var)); qInfo() << "Created:" << thread->objectName(); connect(thread,&QThread::started,this, &Manager::started); connect(thread,&QThread::finished,this, &Manager::finished); threads.append(thread); } } //Start void Manager::start() { qInfo() << "Starting..."; foreach (QThread* thread, threads) { Counter* c = new Counter(); c->moveToThread(thread); connect(thread,&QThread::started, c, &Counter::start); thread->start(); } } void Manager::started() { QThread* thread = qobject_cast<QThread*>(sender()); if(!thread) return; qInfo() << "started:" << thread->objectName(); } void Manager::finished() { QThread* thread = qobject_cast<QThread*>(sender()); if(!thread) return; qInfo() << "finished:" << thread->objectName(); } main.cpp: #include <QCoreApplication> #include <QThread> #include <QDebug> #include "manager.h" int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); QThread::currentThread()->setObjectName("MainThread"); qInfo() << "Current Thread:" << QThread::currentThread()->objectName(); Manager m; m.start(); return a.exec(); } --- result: 00:17:32: C:\Users\Administrator\Documents\build-threadTest-Desktop_Qt_6_5_2_MSVC2019_64bit-Debug\threadTest.exe crashed. 00:17:42: Starting C:\Users\Administrator\Documents\build-threadTest-Desktop_Qt_6_5_2_MSVC2019_64bit-Debug\threadTest.exe... Current Thread: "MainThread" Created: "thread0" Created: "thread1" Created: "thread2" Created: "thread3" Created: "thread4" Starting... [ "thread2" ] 0 [ "thread4" ] 0 started: "thread2" [ "thread4" ] 1 [ "thread1" ] 0 [ "thread0" ] 0 [ "thread3" ] 0 started: "thread0" started: "thread4" started: "thread1" started: "thread3" [ "thread1" ] 1 [ "thread4" ] 2 [ "thread3" ] 1 [ "thread2" ] 1 [ "thread1" ] 2 [ "thread2" ] 2 [ "thread0" ] 1 [ "thread1" ] 3 [ "thread3" ] 2 [ "thread3" ] 3 [ "thread2" ] 3 [ "thread4" ] 3 [ "thread2" ] 4 [ "thread3" ] 4 [ "thread0" ] 2 [ "thread1" ] 4 [ "thread2" ] 5 [ "thread1" ] 5 [ "thread4" ] 4 [ "thread3" ] 5 [ "thread3" ] 6 [ "thread1" ] 6 [ "thread0" ] 3 [ "thread3" ] 7 [ "thread3" ] 8 [ "thread2" ] 6 [ "thread1" ] 7 [ "thread4" ] 5 [ "thread1" ] 8 [ "thread2" ] 7 [ "thread0" ] 4 [ "thread4" ] 6 [ "thread3" ] 9 [ "thread0" ] 5 [ "thread0" ] 6 [ "thread4" ] 7 [ "thread2" ] 8 [ "thread4" ] 8 [ "thread0" ] 7 [ "thread2" ] 9 [ "thread3" ] 10 [ "thread1" ] 9 [ "thread0" ] 8 [ "thread4" ] 9 [ "thread3" ] 11 [ "thread2" ] 10 [ "thread1" ] 10 [ "thread0" ] 9 [ "thread4" ] 10 [ "thread2" ] 11 [ "thread3" ] 12 [ "thread0" ] 10 [ "thread0" ] 11 [ "thread1" ] 11 [ "thread4" ] 11 [ "thread1" ] 12 [ "thread3" ] 13 [ "thread0" ] 12 [ "thread2" ] 12 [ "thread0" ] 13 [ "thread4" ] 12 [ "thread2" ] 13 [ "thread1" ] 13 [ "thread1" ] 14 [ "thread3" ] 14 [ "thread1" ] 15 [ "thread3" ] 15 [ "thread0" ] 14 [ "thread4" ] 13 [ "thread2" ] 14 [ "thread2" ] 15 [ "thread3" ] 16 [ "thread1" ] 16 [ "thread2" ] 16 [ "thread0" ] 15 [ "thread4" ] 14 [ "thread3" ] 17 [ "thread3" ] 18 [ "thread4" ] 15 [ "thread1" ] 17 [ "thread2" ] 17 [ "thread1" ] 18 [ "thread0" ] 16 [ "thread4" ] 16 [ "thread0" ] 17 [ "thread2" ] 18 [ "thread3" ] 19 [ "thread1" ] 19 [ "thread4" ] 17 [ "thread2" ] 19 finished: "thread3" [ "thread0" ] 18 finished: "thread1" [ "thread4" ] 18 finished: "thread2" [ "thread4" ] 19 [ "thread0" ] 19 finished: "thread4" finished: "thread0" 目之所及,皆是回忆,心之所想,皆是过往 分享这篇帖子 链接帖子 分享到其他站点