偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

詳細(xì)了解Thread Affinity與跨線程信號(hào)槽

移動(dòng)開發(fā)
關(guān)于Thread Affinity與跨線程信號(hào)槽,我們來(lái)看內(nèi)容。我們?cè)谌魏螘r(shí)間都可以通過(guò)調(diào)用QObject::thread()來(lái)查詢線程依附性,它適用于構(gòu)建在QThread對(duì)象構(gòu)造函數(shù)的對(duì)象。

本篇介紹詳細(xì)了解Thread Affinity與跨線程信號(hào)槽,QObject的線程依附性(thread affinity)是指某個(gè)對(duì)象的生命周期依附的線程(該對(duì)象生存在該線程里)。我們?cè)谌魏螘r(shí)間都可以通過(guò)調(diào)用QObject::thread()來(lái)查詢線程依附性,它適用于構(gòu)建在QThread對(duì)象構(gòu)造函數(shù)的對(duì)象。

  1. // file multiSignal.h     
  2. #ifndef MULTISIGNAL_H     
  3. #define MULTISIGNAL_H     
  4. #include <QThread>     
  5. class Thread : public QThread     
  6. {     
  7. Q_OBJECT     
  8. signals:     
  9.     void aSignal();     
  10. protected:     
  11.     void run();     
  12. };     
  13. class Object: public QObject     
  14. {     
  15. Q_OBJECT     
  16.    public slots:     
  17.      void aSlot();     
  18. };     
  19. #endif // MULTISIGNAL_H    
  20. // file multiSignal.h  
  21. #ifndef MULTISIGNAL_H  
  22. #define MULTISIGNAL_H  
  23. #include <QThread> 
  24. class Thread : public QThread  
  25. {  
  26. Q_OBJECT  
  27. signals:  
  28.     void aSignal();  
  29. protected:  
  30.     void run();  
  31. };  
  32. class Object: public QObject  
  33. {  
  34. Q_OBJECT  
  35.    public slots:  
  36.      void aSlot();  
  37. };  
  38. #endif // MULTISIGNAL_H  
  39.    
  40.  
  41. view plaincopy to clipboardprint?  
  42. #include <QtCore/QCoreApplication>     
  43. #include <QDebug>     
  44. #include <QTimer>     
  45. #include "multiSignal.h"     
  46. void Object::aSlot()     
  47. {     
  48.    QTimer *timer = new QTimer;     
  49.    qDebug()<< "aSlot " << timer->thread();     
  50.    qDebug() << "aSlot called";     
  51.    delete timer;     
  52. }     
  53. void Thread::run()     
  54. {     
  55.    QTimer *timer = new QTimer;     
  56.    qDebug()<< "run " << timer->thread();     
  57.    emit aSignal();     
  58.    delete timer;     
  59. }     
  60. int main(int argc, char *argv[])     
  61. {     
  62.     QCoreApplication a(argc, argv);     
  63.     Thread thread;     
  64.     Object obj;     
  65.     qDebug()<< "mainThread " << a.thread();     
  66.     qDebug()<< "thread " << thread.thread();     
  67.     qDebug()<< "Object " << obj.thread();     
  68.     QObject::connect(&thread, SIGNAL(aSignal()), &obj, SLOT(aSlot()));     
  69.     thread.start();     
  70.     return a.exec();     
  71. }    
  72. #include <QtCore/QCoreApplication> 
  73. #include <QDebug> 
  74. #include <QTimer> 
  75. #include "multiSignal.h"  
  76. void Object::aSlot()  
  77. {  
  78.    QTimer *timer = new QTimer;  
  79.    qDebug()<< "aSlot " << timer->thread();  
  80.    qDebug() << "aSlot called";  
  81.    delete timer;  
  82. }  
  83. void Thread::run()  
  84. {  
  85.    QTimer *timer = new QTimer;  
  86.    qDebug()<< "run " << timer->thread();  
  87.    emit aSignal();  
  88.    delete timer;  
  89. }  
  90. int main(int argc, char *argv[])  
  91. {  
  92.     QCoreApplication a(argc, argv);  
  93.     Thread thread;  
  94.     Object obj;  
  95.     qDebug()<< "mainThread " << a.thread();  
  96.     qDebug()<< "thread " << thread.thread();  
  97.     qDebug()<< "Object " << obj.thread();  
  98.     QObject::connect(&thread, SIGNAL(aSignal()), &obj, SLOT(aSlot()));  
  99.     thread.start();  
  100.     return a.exec();  

打印結(jié)果:

  1. Debugging starts   
  2. mainThread QThread(0x3e2870)   
  3. thread QThread(0x3e2870)   
  4. Object QThread(0x3e2870)   
  5. run Thread(0x22ff1c)   
  6. aSlot QThread(0x3e2870)   
  7. aSlot called  

我們知道跨線程的信號(hào)槽連接需要使用queued connection,   上述代碼中QObject::connect(&thread, SIGNAL(aSignal()), &obj, SLOT(aSlot()));  雖然thread與obj的線程依附性相同,它們都隸屬于 地址為0x3e2870的線程;  但是我們看到發(fā)射aSignal的線程

與之不同是0x22ff1c. 這就是為什么使用queued connection。

小結(jié):關(guān)于詳細(xì)了解Thread Affinity與跨線程信號(hào)槽的內(nèi)容介紹完了,希望本文對(duì)你有所幫助!

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

2009-07-06 16:05:50

JSP特點(diǎn)

2022-03-08 08:44:13

偏向鎖Java內(nèi)置鎖

2021-04-13 09:07:33

InnoDB內(nèi)存結(jié)構(gòu)

2010-04-16 11:08:23

2010-11-16 09:55:12

Oracle分區(qū)索引

2011-06-15 14:38:01

QT 信號(hào)

2011-07-28 10:40:40

Cocoa KVO

2011-06-07 11:21:04

JSP隱含對(duì)象

2021-07-22 06:08:43

SQL.js關(guān)系數(shù)據(jù)庫(kù)數(shù)據(jù)庫(kù)

2011-08-25 15:10:49

LUAWindows環(huán)境配置

2011-06-09 09:45:35

Linux QT 信號(hào)

2010-10-25 11:51:05

Oracle單行字符串

2010-10-21 15:26:35

SQL Server字

2010-11-12 14:29:46

Sql Server創(chuàng)

2018-11-27 15:55:21

TCP通訊協(xié)議

2011-06-13 10:21:25

QT 信號(hào) 槽機(jī)制

2022-06-07 07:37:40

線程進(jìn)程開發(fā)

2011-06-23 14:40:13

Qt 信號(hào)

2021-06-12 07:38:21

Linkerd 2.Service Mes微服務(wù)

2011-06-20 15:40:19

QT 信號(hào)
點(diǎn)贊
收藏

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