Qt中另一种创建线程的方式
生活随笔
收集整理的這篇文章主要介紹了
Qt中另一种创建线程的方式
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
文章目錄
- 1 Qt中另一種創(chuàng)建線程的方式
- 1.1 另一種創(chuàng)建線程的方式
- 1.2 同步型線程的設(shè)計
- 1.3 異步型線程的設(shè)計
1 Qt中另一種創(chuàng)建線程的方式
1.1 另一種創(chuàng)建線程的方式
歷史的痕跡:
現(xiàn)代軟件架構(gòu)技術(shù)中的參考準(zhǔn)則:
- 盡量使用組合的方式實現(xiàn)系統(tǒng)功能。
- 代碼中僅體現(xiàn)需求中的集成關(guān)系。
思考:
- 通過集成的方式實現(xiàn)新的線程類有什么實際意義?
事實上:
結(jié)論:
- 通過繼承的方式實現(xiàn)多線程沒有任何實際意義。
- QThread對應(yīng)于操作系統(tǒng)中的線程。
- QThread用于充當(dāng)一個線程操作的集合。
- 應(yīng)該提供靈活的方式指定線程入口函數(shù)。
- 盡量避免重寫void run()。
在新版本的QThread類的實現(xiàn)中:
問題:
- 如何靈活的指定一個線程對象的線程入口函數(shù)?
解決方案-信號與槽:
1.2 同步型線程的設(shè)計
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_HAnotherThread.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 異步型線程的設(shè)計
核心要點是在線程結(jié)束后調(diào)用deletLater函數(shù)。
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(); }參考資料:
總結(jié)
以上是生活随笔為你收集整理的Qt中另一种创建线程的方式的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Qt中线程的生命期问题
- 下一篇: 家具板品牌排行有谁熟悉?麻烦介绍下。