详细了解Thread Affinity与跨线程信号槽

移动开发
关于Thread Affinity与跨线程信号槽,我们来看内容。我们在任何时间都可以通过调用QObject::thread()来查询线程依附性,它适用于构建在QThread对象构造函数的对象。

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

  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();  

打印结果:

  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与跨线程信号槽的内容介绍完了,希望本文对你有所帮助!

责任编辑:zhaolei 来源: 互联网
相关推荐

2009-07-06 16:05:50

JSP特点

2022-03-08 08:44:13

偏向锁Java内置锁

2021-04-13 09:07:33

InnoDB内存结构

2011-06-15 14:38:01

QT 信号

2010-04-16 11:08:23

2010-11-16 09:55:12

Oracle分区索引

2011-07-28 10:40:40

Cocoa KVO

2011-06-07 11:21:04

JSP隐含对象

2021-07-22 06:08:43

SQL.js关系数据库数据库

2011-08-25 15:10:49

LUAWindows环境配置

2011-06-09 09:45:35

Linux QT 信号

2010-10-25 11:51:05

Oracle单行字符串

2010-10-21 15:26:35

SQL Server字

2018-11-27 15:55:21

TCP通讯协议

2010-11-12 14:29:46

Sql Server创

2011-06-13 10:21:25

QT 信号 槽机制

2011-06-23 14:40:13

Qt 信号

2022-06-07 07:37:40

线程进程开发

2021-06-12 07:38:21

Linkerd 2.Service Mes微服务

2011-06-20 15:40:19

QT 信号
点赞
收藏

51CTO技术栈公众号