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

来源:互联网 发布:flux软件怎么恢复 编辑:程序博客网 时间:2024/05/31 19:12
 

本篇介绍详细了解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. view plaincopy to clipboardprint?
  40. #include <QtCore/QCoreApplication>
  41. #include <QDebug>
  42. #include <QTimer>
  43. #include "multiSignal.h"
  44. void Object::aSlot()
  45. {
  46. QTimer *timer = new QTimer;
  47. qDebug()<< "aSlot " << timer->thread();
  48. qDebug() << "aSlot called";
  49. delete timer;
  50. }
  51. void Thread::run()
  52. {
  53. QTimer *timer = new QTimer;
  54. qDebug()<< "run " << timer->thread();
  55. emit aSignal();
  56. delete timer;
  57. }
  58. int main(int argc, char *argv[])
  59. {
  60. QCoreApplication a(argc, argv);
  61. Thread thread;
  62. Object obj;
  63. qDebug()<< "mainThread " << a.thread();
  64. qDebug()<< "thread " << thread.thread();
  65. qDebug()<< "Object " << obj.thread();
  66. QObject::connect(&thread, SIGNAL(aSignal()), &obj, SLOT(aSlot()));
  67. thread.start();
  68. return a.exec();
  69. }
  70. #include <QtCore/QCoreApplication>
  71. #include <QDebug>
  72. #include <QTimer>
  73. #include "multiSignal.h"
  74. void Object::aSlot()
  75. {
  76. QTimer *timer = new QTimer;
  77. qDebug()<< "aSlot " << timer->thread();
  78. qDebug() << "aSlot called";
  79. delete timer;
  80. }
  81. void Thread::run()
  82. {
  83. QTimer *timer = new QTimer;
  84. qDebug()<< "run " << timer->thread();
  85. emit aSignal();
  86. delete timer;
  87. }
  88. int main(int argc, char *argv[])
  89. {
  90. QCoreApplication a(argc, argv);
  91. Thread thread;
  92. Object obj;
  93. qDebug()<< "mainThread " << a.thread();
  94. qDebug()<< "thread " << thread.thread();
  95. qDebug()<< "Object " << obj.thread();
  96. QObject::connect(&thread, SIGNAL(aSignal()), &obj, SLOT(aSlot()));
  97. thread.start();
  98. return a.exec();
  99. }

打印结果:

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

原创粉丝点击