详细了解Thread Affinity与跨线程信号槽
创始人
2024-08-02 08:20:37
0

本篇介绍详细了解Thread Affinity与跨线程信号槽,QObject的线程依附性(thread affinity)是指某个对象的生命周期依附的线程(该对象生存在该线程里)。我们在任何时间都可以通过调用QObject::thread()来查询线程依附性,它适用于构建在QThread对象构造函数的对象。

  1. // file multiSignal.h     
  2. #ifndef MULTISIGNAL_H     
  3. #define MULTISIGNAL_H     
  4. #include      
  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  
  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      
  43. #include      
  44. #include      
  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  
  73. #include  
  74. #include  
  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();  

打印结果:

  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  

我们知道跨线程的信号槽连接需要使用queued connection,   上述代码中QObject::connect(&thread, SIGNAL(aSignal()), &obj, SLOT(aSlot()));  虽然thread与obj的线程依附性相同,它们都隶属于 地址为0x3e2870的线程;  但是我们看到发射aSignal的线程

与之不同是0x22ff1c. 这就是为什么使用queued connection。

小结:关于详细了解Thread Affinity与跨线程信号槽的内容介绍完了,希望本文对你有所帮助!

相关内容

热门资讯

PHP新手之PHP入门 PHP是一种易于学习和使用的服务器端脚本语言。只需要很少的编程知识你就能使用PHP建立一个真正交互的...
网络中立的未来 网络中立性是什... 《牛津词典》中对“网络中立”的解释是“电信运营商应秉持的一种原则,即不考虑来源地提供所有内容和应用的...
各种千兆交换机的数据接口类型详... 千兆交换机有很多值得学习的地方,这里我们主要介绍各种千兆交换机的数据接口类型,作为局域网的主要连接设...
粉嫩如何诠释霸道 东芝M805... “霸道粉”是个什么玩意东芝M805拿过来的时候,笔者扑哧笑了,不是笑这款笔记本,而是笑这款产品的颜色...
什么是大数据安全 什么是大数据... 在《为什么需要大数据安全分析》一文中,我们已经阐述了一个重要观点,即:安全要素信息呈现出大数据的特征...
全面诠释网络负载均衡 负载均衡的出现大大缓解了服务器的压力,更是有效的利用了资源,提高了效率。那么我们现在来说一下网络负载...
如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...
如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
30分钟搞定iOS自定义相机 最近公司的项目中用到了相机,由于不用系统的相机,UI给的相机切图,必须自定义才可以。就花时间简单研究...
P2P的自白|我不生产内容,我... 现在一提起P2P,人们就会联想到正在被有关部门“围剿”的互联网理财服务。×租宝事件使得劳...