自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

淺談 Qt 線程類 起點學(xué)習(xí)

移動開發(fā)
本文介紹的是理解 Qt 多線程類 起點學(xué)習(xí),QThread 是Qt中一個對線程支持的核心的底層類。每個線程對象代表了一個運行的線程。

淺談 Qt 多線程類 起點學(xué)習(xí)是本文要介紹的內(nèi)容,不多說,先來看內(nèi)容。由于Qt的跨平臺特性,QThread成功隱藏了所有在不同操作系統(tǒng)里使用線程的平臺相關(guān)性代碼。

POINT 1:QThread的實例與普通的實例沒什么不同,只是運行著的run()函數(shù)會不同。

例1:

  1. class MThread :public QThread   
  2. {   
  3. public:   
  4.     MThread();   
  5.     ~MThread();   
  6.     void run();   
  7.     void foo();   
  8.     ...   
  9.        
  10. };  
  11. class MDialog :public QDialog   
  12. {   
  13.     ...   
  14.     MThread *mythread;   
  15. };   
  16. MDialog::MDialog()   
  17. {   
  18.     mythread = new MThread;   
  19.     ...    

需要注意的是,在QT中,QThread對象的實例mythread是屬于創(chuàng)建它的線程(線程A,即MDialog所在的線程)的,mythread的所有程序代碼與數(shù)據(jù)都放在與MDialog相同的空間中.這時的mythread,就像任何普通的自己定義的類的實例一樣.但是在調(diào)用mythread->start()之后,mythread的run()函數(shù)中的代碼會在新的線程(線程B)中執(zhí)行.在run()函數(shù)中聲明的變量\實例化的對象,都屬于線程B.

但是mythread的所有代碼,都還在存儲在線程A中,只是run()函數(shù)的"執(zhí)行"是在線程B中.在MDialog中,使用mythread->foo();foo()是在線程A中執(zhí)行的.在MDialog中使用connect(this, SIGNAL(sigDialogSignal()), mythread, SLOT(slotThreadSlot()));

當(dāng)emit sigDialogSignal()時,是會在MDialog所在的線程A中執(zhí)行的.因為mythread與MDialog同屬于一個線程, 這時thread可以看做一個普通的實例.另外,因為connect函數(shù)的連接方式默認(rèn)是自動連接,而對同屬于一個純種的兩個對象,自動連接會使用直接連接,即slot在發(fā)出signal的線程中立即執(zhí)行.

例2:

  1. #include "mthread.h"   
  2. #include <QDebug>   
  3. MThread::MThread(QObject *parent)   
  4.     : QThread(parent)   
  5. {   
  6.     myTimer.start(1);   
  7.     connect(&myTimer, SIGNAL(timeout()), this, SLOT(slotPrint()));   
  8. }   
  9.  
  10. MThread::~MThread()   
  11. {   
  12.  
  13. }   
  14. void MThread::run()   
  15. {      
  16.     for (int i = 0; i < 100; ++i) {   
  17.         for (int j = 0 ; j < 10000; ++j) {   
  18.             qDebug()<<"---------"<<i;   
  19.         }   
  20.     }   
  21.     exec();   
  22. }   
  23. void MThread::slotPrint()   
  24. {   
  25.     qDebug()<<"==============================";   
  26.  

運行后出現(xiàn):

  1. ...   
  2. ...   
  3. ---------9   
  4. ==============================================================   
  5. ---------9   
  6. ...   
  7. ... 

不能誤以為:在一個QThread類的派生類中,run()函數(shù)中的語句在運行時,可能被本線程定時器超時slot中斷. (錯誤)

事實上,slotPrint()在創(chuàng)建MThread的實例的線程中執(zhí)行.

POINT 2:線程B中的對象要想接收線程A中的對象發(fā)來的signal, 必須進(jìn)入exec(), 如在exec()前有死循環(huán), 沒有進(jìn)入exec(), 則線程B中的對象不會收到signal.

  1. void MThread::run()   
  2. {   
  3.     while(1) {   
  4.         dosomething();  //此循環(huán)永不退出   
  5.     }   
  6.     exec();             //如果此事件循環(huán)不能進(jìn)入,剛此線程不會收到任何signal   

POINT 3:線程A中的指針可指向線程B中創(chuàng)建的對象實例,  這個實例屬于線程B. 指針僅僅是一個地址, 而對象實例的變量/代碼等都屬于線程B.

例1:

  1. class MThread : public QThread   
  2. {   
  3.     Q_OBJECT   
  4. public:   
  5.     MThread(QObject *parent = 0);   
  6.     ~MThread();   
  7.     void run();   
  8.     MPrint *mprint;   
  9. };   
  10. void MThread::run()   
  11. {   
  12.     mprint = new MPrint;   
  13.     exec();   
  14. }   
  15. //如此聲明,mprint所指向的對象屬于另一個線程. 

例2:

  1. class MThread : public QThread   
  2. {   
  3.     Q_OBJECT   
  4.  
  5. public:   
  6.     MThread(QObject *parent = 0);   
  7.     ~MThread();   
  8.     void run();   
  9.     MPrint *mprint;   
  10. private:   
  11.     QTimer *myTimer;   
  12. private slots:   
  13.     void slotPrint();      
  14.     void testFoo();   
  15. };   
  16. void MThread::run()   
  17. {   
  18.     myTimer = new QTimer;   
  19.     mprint = new MPrint;   
  20.     myTimer->setInterval(100);   
  21.     connect(myTimer, SIGNAL(timeout()), this, SLOT(testFoo()), Qt::DirectConnection);   
  22.     QTimer::singleShot(0, myTimer,SLOT(start()));   
  23.     exec();   

 

上這樣寫run(),myTimer在run()中new,即myTimer這個指針屬于舊線程,但myTimer所指向的QTimer實例的實體在新的線程中,testFoo()會在新線程中執(zhí)行,例3:

  1. void MThread::run()   
  2. {   
  3.     QTimer myTimer;   
  4.     mprint = new MPrint;   
  5.     myTimer.setInterval(100);   
  6.     connect(&myTimer, SIGNAL(timeout()), this, SLOT(testFoo()), Qt::DirectConnection);   
  7.     QTimer::singleShot(0, &myTimer,SLOT(start()));   
  8.     //testFoo();   
  9.     exec();   

以上這樣寫run(),myTimer在run()中聲明,即myTimer屬于新的線程,testFoo()也會在新線程中執(zhí)行,例4:

  1. class MThread : public QThread   
  2. {   
  3.     Q_OBJECT   
  4. public:   
  5.     MThread(QObject *parent = 0);   
  6.     ~MThread();   
  7.     void run();   
  8.     MPrint *mprint;   
  9. private:   
  10.     QTimer myTimer;   
  11. private slots:   
  12.     void slotPrint();      
  13.     void testFoo();   
  14. };   
  15. void MThread::run()   
  16. {   
  17.     mprint = new MPrint;   
  18.     myTimer.setInterval(100);   
  19.     connect(&myTimer, SIGNAL(timeout()), this, SLOT(testFoo()));   
  20.     QTimer::singleShot(0, &myTimer,SLOT(start()));   
  21.     //testFoo();   
  22.     exec();   

以上這樣寫run(),testFoo()會在創(chuàng)建myTimer的老線程中執(zhí)行.因為可以看到,mytimer和this(即mythread),都是在同一個線程中,只是在另一個線程中(run()),做了connect操作.

要注意的是,在線程B中啟動線程A中的一個定時器,不能使用myTimer.start(),這樣啟動不了定時器.而應(yīng)使用signal來觸發(fā)start()這個slot.

POINT 5:slot不會中斷同線程中的slot,例1:

  1. #include "mthread.h"   
  2. #include <QDebug>   
  3. MThread::MThread(QObject *parent)   
  4.     : QThread(parent)   
  5. {   
  6.     myTimer.start(1);   
  7.     connect(&myTimer, SIGNAL(timeout()), this, SLOT(slotPrint()));   
  8. }   
  9. MThread::~MThread()   
  10. {   
  11.  
  12. }   
  13. void MThread::run()   
  14. {   
  15.     exec();   
  16. }   
  17. void MThread::slotPrint()   
  18. {   
  19.     qDebug()<<"===========================";   
  20.     for (int i = 0; i < 100; ++i) {   
  21.         for (int j = 0 ; j < 10000; ++j) {   
  22.             qDebug()<<"---------"<<i;   
  23.         }   
  24.     }   
  25. }  

slotPrint()函數(shù)運行完之后才會退出,說明slot不會中斷slot,一個slot在執(zhí)行完之后才會執(zhí)行下一個slot.

注意:slotPrint()在創(chuàng)建MThread實例的線程中執(zhí)行.而不是使用thread->start()創(chuàng)建出的那個線程,例2:

  1. #include "mthread.h"   
  2. #include <QDebug>   
  3. MThread::MThread(QObject *parent)   
  4.     : QThread(parent)   
  5. {   
  6.     myTimer.start(1);   
  7.     connect(&myTimer, SIGNAL(timeout()), this, SLOT(slotPrint()));   
  8. }   
  9. MThread::~MThread()   
  10. {   
  11.  
  12. }   
  13. void MThread::run()   
  14. {   
  15.     testFoo();   
  16.     exec();   
  17. }   
  18.  
  19. void MThread::slotPrint()   
  20. {   
  21.     qDebug()<<"=======================";   
  22.  
  23. }   
  24. void MThread::testFoo()   
  25. {   
  26.     for (int i = 0; i < 100; ++i) {   
  27.         for (int j = 0 ; j < 10000; ++j) {   
  28.             qDebug()<<"---------"<<i;   
  29.         }   
  30.     }   
  31. }  

以上代碼中,slotPrint()與testFoo()會在兩個不同的線程中執(zhí)行.

小結(jié):淺談 Qt 多線程類 起點學(xué)習(xí)的內(nèi)容介紹完了,希望本文對你有所幫助。更多內(nèi)容請參考編輯推薦。

責(zé)任編輯:zhaolei 來源: 互聯(lián)網(wǎng)
相關(guān)推薦

2011-07-04 16:20:54

QT 窗口 QWidget

2011-06-13 10:03:19

Qt 多線程 編程

2011-06-29 16:34:11

Qt 子線程 線程

2011-09-07 16:36:00

Qt Widget

2011-06-28 14:02:34

QT ARM

2011-06-22 15:24:50

Qt 線程

2011-06-15 10:49:26

Qt QTableItem

2011-07-04 15:43:03

Qt 布局管理器 designer

2011-06-28 15:37:34

Qt 內(nèi)存

2011-06-21 16:51:21

Qt 靜態(tài) 編譯

2011-08-30 13:33:29

Qt數(shù)據(jù)庫

2011-07-05 10:22:44

Qt Sqlite

2011-07-04 15:30:24

Qt 布局 GridLayout

2011-06-15 10:08:01

Qt CVS

2011-06-15 16:50:09

Qt 模塊

2011-06-28 17:21:50

QT UI designer

2011-06-16 11:28:48

Qt QApplicati

2011-08-29 10:34:36

QTQWebKitJavaScript

2011-06-22 10:12:08

Qt 線程

2011-06-10 16:44:17

Qt 瀏覽器
點贊
收藏

51CTO技術(shù)棧公眾號