成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

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

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

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

POINT 1:QThread的實(shí)例與普通的實(shí)例沒什么不同,只是運(yùn)行著的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對象的實(shí)例mythread是屬于創(chuàng)建它的線程(線程A,即MDialog所在的線程)的,mythread的所有程序代碼與數(shù)據(jù)都放在與MDialog相同的空間中.這時(shí)的mythread,就像任何普通的自己定義的類的實(shí)例一樣.但是在調(diào)用mythread->start()之后,mythread的run()函數(shù)中的代碼會在新的線程(線程B)中執(zhí)行.在run()函數(shù)中聲明的變量\實(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()時(shí),是會在MDialog所在的線程A中執(zhí)行的.因?yàn)閙ythread與MDialog同屬于一個(gè)線程, 這時(shí)thread可以看做一個(gè)普通的實(shí)例.另外,因?yàn)閏onnect函數(shù)的連接方式默認(rèn)是自動連接,而對同屬于一個(gè)純種的兩個(gè)對象,自動連接會使用直接連接,即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.  

運(yùn)行后出現(xiàn):

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

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

事實(shí)上,slotPrint()在創(chuàng)建MThread的實(shí)例的線程中執(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)建的對象實(shí)例,  這個(gè)實(shí)例屬于線程B. 指針僅僅是一個(gè)地址, 而對象實(shí)例的變量/代碼等都屬于線程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所指向的對象屬于另一個(gè)線程. 

例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這個(gè)指針屬于舊線程,但myTimer所指向的QTimer實(shí)例的實(shí)體在新的線程中,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í)行.因?yàn)榭梢钥吹?mytimer和this(即mythread),都是在同一個(gè)線程中,只是在另一個(gè)線程中(run()),做了connect操作.

要注意的是,在線程B中啟動線程A中的一個(gè)定時(shí)器,不能使用myTimer.start(),這樣啟動不了定時(shí)器.而應(yīng)使用signal來觸發(fā)start()這個(gè)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ù)運(yùn)行完之后才會退出,說明slot不會中斷slot,一個(gè)slot在執(zhí)行完之后才會執(zhí)行下一個(gè)slot.

注意:slotPrint()在創(chuàng)建MThread實(shí)例的線程中執(zhí)行.而不是使用thread->start()創(chuàng)建出的那個(gè)線程,例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()會在兩個(gè)不同的線程中執(zhí)行.

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

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

2011-07-04 16:20:54

QT 窗口 QWidget

2011-06-29 16:34:11

Qt 子線程 線程

2011-06-13 10:03:19

Qt 多線程 編程

2011-09-07 16:36:00

Qt Widget

2011-06-28 14:02:34

QT ARM

2011-07-04 15:43:03

Qt 布局管理器 designer

2011-06-22 15:24:50

Qt 線程

2011-06-15 10:49:26

Qt QTableItem

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-10 16:44:17

Qt 瀏覽器

2011-07-08 16:43:46

iPhone Cocoa 多線程
點(diǎn)贊
收藏

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

主站蜘蛛池模板: 亚洲一区二区高清 | 国产成人免费视频网站视频社区 | 欧美午夜在线 | 国产毛片久久久 | 精品视频一区二区三区在线观看 | 日韩精品一区二区三区中文在线 | 日韩欧美一区在线 | 成年视频在线观看福利资源 | 免费看日韩视频 | 日本黄色大片免费看 | 久久久www成人免费无遮挡大片 | 日本久久久久久久久 | 精品一区av | 国产精品一区在线观看你懂的 | 91污在线| 一区二区三区av | 欧美激情精品久久久久 | 中文二区 | 国产在线精品一区二区 | 欧洲性生活视频 | 欧美电影网 | 一二区电影 | 国产精彩视频 | 久久久妇女国产精品影视 | 自拍偷拍在线视频 | a级黄色网 | 成人国产精品免费观看 | 一区二区国产在线 | 亚洲精品一区二区三区在线 | 日本一区二区影视 | 欧美成人自拍视频 | 日韩福利电影 | 人人看人人搞 | 国产激情视频在线观看 | 免费的av| 综合五月 | 亚洲乱码一区二区三区在线观看 | 黄色免费网站在线看 | 亚洲三区视频 | 国产日韩精品在线 | 欧美日韩精品区 |