日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Qt中另一种创建线程的方式

發布時間:2025/4/5 编程问答 15 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Qt中另一种创建线程的方式 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

    • 1 Qt中另一種創建線程的方式
      • 1.1 另一種創建線程的方式
      • 1.2 同步型線程的設計
      • 1.3 異步型線程的設計

1 Qt中另一種創建線程的方式

1.1 另一種創建線程的方式

歷史的痕跡:

現代軟件架構技術中的參考準則:

  • 盡量使用組合的方式實現系統功能。
  • 代碼中僅體現需求中的集成關系。

思考:

  • 通過集成的方式實現新的線程類有什么實際意義?

事實上:

結論:

  • 通過繼承的方式實現多線程沒有任何實際意義。
  • QThread對應于操作系統中的線程。
  • QThread用于充當一個線程操作的集合。
  • 應該提供靈活的方式指定線程入口函數。
  • 盡量避免重寫void run()。

在新版本的QThread類的實現中:

問題:

  • 如何靈活的指定一個線程對象的線程入口函數?

解決方案-信號與槽:

  • 在類中定義一個槽函數void tmain()作為線程入口函數。
  • 在類中定義一個QThread成員對象m_thread。
  • 改變當前對象的線程依附性到m_thread。
  • 連接m_thread的start()信號到tmain()(一定不要忘了在tmain中調用quit退出線程)。
  • 1.2 同步型線程的設計

    AnotherThread.h:

    #ifndef ANOTHERTHREAD_H #define ANOTHERTHREAD_H#include <QObject> #include <QThread>class AnotherThread : public QObject {Q_OBJECTQThread m_thread; protected slots:void tmain(); public:explicit AnotherThread(QObject *parent = 0);void start();void terminate();void exit(int c);~AnotherThread();};#endif // ANOTHERTHREAD_H

    AnotherThread.cpp:

    #include "AnotherThread.h" #include <QDebug>AnotherThread::AnotherThread(QObject *parent) :QObject(parent) {moveToThread(&m_thread);connect(&m_thread, SIGNAL(started()), this, SLOT(tmain())); }void AnotherThread::tmain() {qDebug() << "void AnotherThread::tmain() tid = " << QThread::currentThreadId();for(int i=0; i<10; i++){qDebug() << "void AnotherThread::tmain() i = " << i;}qDebug() << "void AnotherThread::tmain() end";m_thread.quit(); }void AnotherThread::start() {m_thread.start(); }void AnotherThread::terminate() {m_thread.terminate(); }void AnotherThread::exit(int c) {m_thread.exit(c); }AnotherThread::~AnotherThread() {m_thread.wait(); }

    main.cpp:

    #include <QtCore/QCoreApplication> #include <QDebug> #include <QThread> #include "AnotherThread.h"void test() {AnotherThread at;at.start(); }int main(int argc, char *argv[]) {QCoreApplication a(argc, argv);qDebug() << "main() tid = " << QThread::currentThreadId();test();return a.exec(); }

    1.3 異步型線程的設計

    核心要點是在線程結束后調用deletLater函數。

    AnotherThread.h:

    #include "AsyncThread.h" #include <QDebug>AsyncThread::AsyncThread(QObject *parent) :QThread(parent) { }void AsyncThread::run() {for (int i=0; i<5; i++){qDebug() << "MyThread run() : " << i << ", thread id = " << currentThreadId();sleep(1);}deleteLater(); }bool AsyncThread::event(QEvent *e) {qDebug() << "void AsyncThread::event(QEvent *)";return QThread::event(e); }int AsyncThread::exec() {qDebug() << "int AsyncThread::exec()";return QThread::exec(); }AsyncThread::~AsyncThread() {qDebug() << "AsyncThread::~AsyncThread() : " << currentThreadId(); }AsyncThread *AsyncThread::NewInstance() {return new AsyncThread; }

    AnotherThread.cpp:

    #include "AnotherThread.h" #include <QDebug>AnotherThread::AnotherThread(QObject *parent) :QObject(parent) {m_pThread = QThread::currentThread();moveToThread(&m_thread);connect(&m_thread, SIGNAL(started()), this, SLOT(tmain())); }AnotherThread::~AnotherThread() {if (m_thread.isRunning()){m_thread.wait();}qDebug() << "AnotherThread::~AnotherThread() : " << QThread::currentThreadId(); }AnotherThread *AnotherThread::NewInstance() {return new AnotherThread; }void AnotherThread::tmain() {for (int i=0; i<5; i++){qDebug() << "void AnotherThread::tmain() : " << i << ", thread id = " << QThread::currentThreadId();}m_thread.quit();moveToThread(m_pThread);deleteLater(); }void AnotherThread::onFinished() {qDebug() << "void AnotherThread::finished()";//delete this; }void AnotherThread::start() {m_thread.start(); }void AnotherThread::exit(int c) {m_thread.exit(c); }void AnotherThread::quit() {m_thread.quit(); }bool AnotherThread::wait(unsigned long t) {return m_thread.wait(t); }void AnotherThread::terminate() {m_thread.terminate(); }

    參考資料:

  • QT實驗分析教程
  • 總結

    以上是生活随笔為你收集整理的Qt中另一种创建线程的方式的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。