详细了解Thread Affinity与跨线程信号槽
来源:互联网 发布:flux软件怎么恢复 编辑:程序博客网 时间:2024/05/31 19:12
本篇介绍详细了解Thread Affinity与跨线程信号槽,QObject的线程依附性(thread affinity)是指某个对象的生命周期依附的线程(该对象生存在该线程里)。我们在任何时间都可以通过调用QObject::thread()来查询线程依附性,它适用于构建在QThread对象构造函数的对象。
- // file multiSignal.h
- #ifndef MULTISIGNAL_H
- #define MULTISIGNAL_H
- #include <QThread>
- class Thread : public QThread
- {
- Q_OBJECT
- signals:
- void aSignal();
- protected:
- void run();
- };
- class Object: public QObject
- {
- Q_OBJECT
- public slots:
- void aSlot();
- };
- #endif // MULTISIGNAL_H
- // file multiSignal.h
- #ifndef MULTISIGNAL_H
- #define MULTISIGNAL_H
- #include <QThread>
- class Thread : public QThread
- {
- Q_OBJECT
- signals:
- void aSignal();
- protected:
- void run();
- };
- class Object: public QObject
- {
- Q_OBJECT
- public slots:
- void aSlot();
- };
- #endif // MULTISIGNAL_H
- view plaincopy to clipboardprint?
- #include <QtCore/QCoreApplication>
- #include <QDebug>
- #include <QTimer>
- #include "multiSignal.h"
- void Object::aSlot()
- {
- QTimer *timer = new QTimer;
- qDebug()<< "aSlot " << timer->thread();
- qDebug() << "aSlot called";
- delete timer;
- }
- void Thread::run()
- {
- QTimer *timer = new QTimer;
- qDebug()<< "run " << timer->thread();
- emit aSignal();
- delete timer;
- }
- int main(int argc, char *argv[])
- {
- QCoreApplication a(argc, argv);
- Thread thread;
- Object obj;
- qDebug()<< "mainThread " << a.thread();
- qDebug()<< "thread " << thread.thread();
- qDebug()<< "Object " << obj.thread();
- QObject::connect(&thread, SIGNAL(aSignal()), &obj, SLOT(aSlot()));
- thread.start();
- return a.exec();
- }
- #include <QtCore/QCoreApplication>
- #include <QDebug>
- #include <QTimer>
- #include "multiSignal.h"
- void Object::aSlot()
- {
- QTimer *timer = new QTimer;
- qDebug()<< "aSlot " << timer->thread();
- qDebug() << "aSlot called";
- delete timer;
- }
- void Thread::run()
- {
- QTimer *timer = new QTimer;
- qDebug()<< "run " << timer->thread();
- emit aSignal();
- delete timer;
- }
- int main(int argc, char *argv[])
- {
- QCoreApplication a(argc, argv);
- Thread thread;
- Object obj;
- qDebug()<< "mainThread " << a.thread();
- qDebug()<< "thread " << thread.thread();
- qDebug()<< "Object " << obj.thread();
- QObject::connect(&thread, SIGNAL(aSignal()), &obj, SLOT(aSlot()));
- thread.start();
- return a.exec();
- }
打印结果:
- Debugging starts
- mainThread QThread(0x3e2870)
- thread QThread(0x3e2870)
- Object QThread(0x3e2870)
- run Thread(0x22ff1c)
- aSlot QThread(0x3e2870)
- aSlot called
我们知道跨线程的信号槽连接需要使用queued connection, 上述代码中QObject::connect(&thread, SIGNAL(aSignal()), &obj, SLOT(aSlot())); 虽然thread与obj的线程依附性相同,它们都隶属于 地址为0x3e2870的线程; 但是我们看到发射aSignal的线程
与之不同是0x22ff1c. 这就是为什么使用queued connection。
小结:关于详细了解Thread Affinity与跨线程信号槽的内容介绍完了,希望本文对你有所帮助!
- 详细了解Thread Affinity与跨线程信号槽
- 深入了解信号与槽
- 跨线程的信号与槽
- QT跨线程的信号与槽
- QObject 之 Thread Affinity
- QObject 之 Thread Affinity
- QObject 之 Thread Affinity
- QT跨线程的信号与槽[2]---后台SOCKET
- c# Thread 线程详细讲解
- 深入了解信号槽
- 深入了解信号槽
- linux进程、线程与cpu的亲和性(affinity)
- 线程与信号
- 线程与信号
- 线程与信号
- 线程与信号
- 信号与线程
- 线程与信号
- Net Framework各个版本区别
- android webview经验总结
- android开发中的经验
- Qt事件处理机制
- 怎样停止AsyncTask和Thread
- 详细了解Thread Affinity与跨线程信号槽
- Qt智能指针使用指南 帮助篇
- 早上+job+.
- Davinci DM6446开发攻略——LINUX GPIO驱动源码移植
- 分享:自主研发搜索引擎,单机500万网页全文索引,任意检索不超过 20毫秒 .
- ORACLE 高级函数应用
- “键入的打印机名不正确、打印机没有连接到服务器上”终极解决方法
- SetModifiedFlag( BOOL bModified = TRUE )的MSDN解析
- 分享一段PHP重定向网址代码