注意,本文试图通过源码解释下面的问题:
AutoConnection的是是非非,两种说法孰是孰非?
但很显然,我没做到这一点(能力所限,现阶段我只能让自己勉强明白),尽管如此,本文应该还是提供了很多你理解这些问题所需的背景知识。
线程关联性(Thread Affinity)???
什么东西? |
每一个QObject都会和一个线程相关联 |
QObject 是线程感知的,每一个QObject及派生类的对象被创建时都会将其所在线程的引用保存下来(可以通过QObject::thread()返回)。 |
干嘛用的? |
用于事件系统 |
QObject对象的事件处理函数始终要在其所关联线程的上下文中执行。 |
可否改变? |
使用QObject::moveToThread()可以将QObject对象从一个线程移动到另一个线程。 |
看看QObject的初始化(看两点):
QObject::QObject(QObject *parent) : d_ptr(new QObjectPrivate) { Q_D(QObject); d->threadData = (parent && !parent->thread()) ? parent->d_func()->threadData : QThreadData::current(); if (parent) { if (!check_parent_thread(parent, parent ? parent->d_func()->threadData : 0, d->threadData)) parent = 0; setParent(parent); }
看看moveToThread()的代码(我们此处只关心限制条件):
void QObject::moveToThread(QThread *targetThread) { Q_D(QObject); if (d->parent != 0) { qWarning("QObject::moveToThread: Cannot move objects with a parent"); return; } if (d->isWidget) { qWarning("QObject::moveToThread: Widgets cannot be moved to a new thread"); return; } QThreadData *currentData = QThreadData::current(); QThreadData *targetData = targetThread ? QThreadData::get2(targetThread) : new QThreadData(0); if (d->threadData->thread == 0 && currentData == targetData) { // one exception to the rule: we allow moving objects with no thread affinity to the current thread currentData = d->threadData; } else if (d->threadData != currentData) { qWarning("QObject::moveToThread: Current thread (%p) is not the object's thread (%p)./n" "Cannot move to target thread (%p)/n", currentData->thread, d->threadData->thread, targetData->thread); return; } ......
moveToThread()的其他工作:
生成并通过sendEvent()派发 QEvent::ThreadChange 事件
我们在QDialog 模态对话框与事件循环 一文中提到:
int QCoreApplication::exec() { ... QEventLoop eventLoop; int returnCode = eventLoop.exec(); ... return returnCode; }
int QEventLoop::exec(ProcessEventsFlags flags) { Q_D(QEventLoop); ... while (!d->exit) processEvents(flags | WaitForMoreEvents | EventLoopExec); ... return d->returnCode; }
bool QEventLoop::processEvents(ProcessEventsFlags flags) { Q_D(QEventLoop); if (!d->threadData->eventDispatcher) return false; if (flags & DeferredDeletion) QCoreApplication::sendPostedEvents(0, QEvent::DeferredDelete); return d->threadData->eventDispatcher->processEvents(flags); }
QEventLoop::QEventLoop(QObject *parent) : QObject(*new QEventLoopPrivate, parent) { Q_D(QEventLoop); if (!d->threadData->eventDispatcher) { QThreadPrivate::createEventDispatcher(d->threadData); } }
一个线程内可以创建并启动多个QEventLoop(事件循环可以嵌套,你经常这样用,只不过可能没意识到,可考虑QEventLoop使用两例 ),而第一个负责创建eventDispatcher.
QCoreApplicationn::postEvent()和线程有什么瓜葛?
void QCoreApplication::postEvent(QObject *receiver, QEvent *event, int priority) { QThreadData * volatile * pdata = &receiver->d_func()->threadData; if (data->postEventList.isEmpty() || data->postEventList.last().priority >= priority) { data->postEventList.append(QPostEvent(receiver, event, priority)); } else { QPostEventList::iterator begin = data->postEventList.begin() + data->postEventList.insertionOffset, end = data->postEventList.end(); QPostEventList::iterator at = qUpperBound(begin, end, priority); data->postEventList.insert(at, QPostEvent(receiver, event, priority)); } if (data->eventDispatcher) data->eventDispatcher->wakeUp(); ...
无论如何,事件最终都要通过 sendEvent 和 sendSpontaneousEvent 才能派发到接收的对象中
send(Spontaneous)Event 直接调用notifyInternal,进而直接调用notify,最终直接调用QObject::event()
inline bool QCoreApplication::sendEvent(QObject *receiver, QEvent *event) { if (event) event->spont = false; return self ? self->notifyInternal(receiver, event) : false; } inline bool QCoreApplication::sendSpontaneousEvent(QObject *receiver, QEvent *event) { if (event) event->spont = true; return self ? self->notifyInternal(receiver, event) : false; }
因为事件由其关联的线程内的eventDispatcher进行派发,所以所有的事件处理函数都会在关联的线程内被调用。如果关联线程的事件循环没有启用呢?就不会有eventispatcher了,timerEvent等事件也就更无从谈起了。
为何只能在其关联的线程内启动timer?
在QTimer源码分析(以Windows下实现为例) 一文中,我们谈到:
QTimer的是通过QObject的timerEvent()实现的,开启和关闭定时器是通过QObject的startTimer()和killTimer完成的。
startTimer最终调用对象关联线程的eventDispatcher来注册定时器:
int QObject::startTimer(int interval) { Q_D(QObject); return d->threadData->eventDispatcher->registerTimer(interval, this);
在Win32平台下:
void QEventDispatcherWin32::registerTimer(int timerId, int interval, QObject *object) { if (timerId < 1 || interval < 0 || !object) { qWarning("QEventDispatcherWin32::registerTimer: invalid arguments"); return; } else if (object->thread() != thread() || thread() != QThread::currentThread()) { qWarning("QObject::startTimer: timers cannot be started from another thread"); return; } ...
在Linux平台下:
void QEventDispatcherGlib::registerTimer(int timerId, int interval, QObject *object) { #ifndef QT_NO_DEBUG if (timerId < 1 || interval < 0 || !object) { qWarning("QEventDispatcherGlib::registerTimer: invalid arguments"); return; } else if (object->thread() != thread() || thread() != QThread::currentThread()) { qWarning("QObject::startTimer: timers cannot be started from another thread"); return; } ...
在这两个平台下,它都会检查当前线程和dispatcher的线程是否一致。不一致则直接返回。
为什么要这么设计。我不太清楚。或许是因为:注册定时器要用到回调函数,而回调函数需要在注册的线程执行(fix me)。
使用connect连接信号槽时,默认是 AutoConnection
使用invokeMethod时,可以指定 AutoConnection
设置AutoConnection就是让Qt帮助我们选择直连还是队列连接的方式。选择的依据就是当前的线程和接收者的关联的线程是否一致,而与信号所在对象关联的线程无关 (对Qt4.8及后续版本,这句话是对的)。
这个不涉及信号的问题,处理起来很简单:比较当前线程和接收者所关联的线程是否一致即可。
检查Connection的类型,处理AutoConnection
// check connection type QThread *currentThread = QThread::currentThread(); QThread *objectThread = object->thread(); if (connectionType == Qt::AutoConnection) { connectionType = currentThread == objectThread ? Qt::DirectConnection : Qt::QueuedConnection; }
if (connectionType == Qt::DirectConnection) { return QMetaObject::metacall(object, QMetaObject::InvokeMetaMethod, methodIndex, param) < 0;
if (connectionType == Qt::QueuedConnection) { QCoreApplication::postEvent(object, new QMetaCallEvent(methodIndex, 0, -1, nargs, types, args));
connect中指定了AutoConnection,信号发射时,相应槽是Direct还是Queued方式调用呢???
你应该见过两种说法:
注意:在Qt4.7.3(包括)以前,前一种说法是对的(充分条件)。从Qt4.8开始,后面的说法是对的(充要条件)。
看看Qt4.7.3中QMetaObject::activate()的代码:
// determine if this connection should be sent immediately or // put into the event queue if ((c->connectionType == Qt::AutoConnection && (currentThreadData != sender->d_func()->threadData || receiver->d_func()->threadData != sender->d_func()->threadData)) || (c->connectionType == Qt::QueuedConnection)) { queued_activate(sender, signal_absolute_index, c, argv ? argv : empty_argv); continue; }
对比看看Qt4.8中的代码:
const bool receiverInSameThread = currentThreadId == receiver->d_func()->threadData->threadId; // determine if this connection should be sent immediately or // put into the event queue if ((c->connectionType == Qt::AutoConnection && !receiverInSameThread) || (c->connectionType == Qt::QueuedConnection)) { queued_activate(sender, signal_absolute_index, c, argv ? argv : empty_argv); continue; }