本文主要是介绍QObject_thread,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
QObject::thread
QThread *QObject::thread()
返回对象所在的线程。
QThread *QObject::thread() const
{return d_func()->threadData.loadRelaxed()->thread.loadAcquire();
}
void QObject::moveToThread(QThread *targetThread)
将此对象及其孩子关联到targetThread线程,事件处理将在targetThread中继续进行,
在线程关联被改变之前QEvent::ThreadChange事件被发送给这个对象和其孩子对象。
注意:
- widget类型(继承QWidget的类)对象只能在主线程中,所以调用moveToThread无效。
- 如果对象有父对象,则不能移动该对象。
- 调用者必须与此对象在同一线程,除非此对象没有关联线程
- 该对象的计时器timer首先在当前线程中停止,然后在targetThread中重新启动(间隔相同)。因此,在线程之间不断移动对象会无限期地延迟计时器事件。
- 如果targetThread为nullptr,则此对象及其子对象的所有事件处理都将停止,因为它们不再与任何线程相关联。
使用样例:
int main()
{QThread *th = new QThread();myobject *obj1= new myobject("boj1");obj1->connect(obj1,&myobject::signal1,obj1,&myobject::slot1);obj1->moveToThread(th);//调用在主线程与obj1同一线程//因为线程启动后才会执行事件循环,所以必须启动线程th->start();qDebug()<<"main QThreadid = "<<QThread::currentThreadId()<<",date="<<QDateTime::currentSecsSinceEpoch();emit obj1->signal1(111);return 0;
}
函数解读:
void QObject::moveToThread(QThread *targetThread)
{Q_D(QObject);//如果对象有父对象,则不能移动该对象。if (d->parent != nullptr) {qWarning("QObject::moveToThread: Cannot move objects with a parent");return;}//对象是widget类型的不能移动widget类型的只能在主线程中//继承QWidget的类都是widget类型//explicit QWidget(QWidget* parent = nullptr, Qt::WindowFlags);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) : nullptr;//对象所在线程数据,QThreadData *thisThreadData = d->threadData.loadRelaxed();//如果对象没有关联线程,并且目标线程等于当前线程,可以移动if (!thisThreadData->thread.loadAcquire() && currentData == targetData){//我们允许将没有线程关联的对象移动到当前线程currentData = d->threadData;} else if (thisThreadData != currentData) {//只能在object所在的线程里面调用此函数qWarning("QObject::moveToThread: Current thread (%p) is not the object's thread (%p).\n""Cannot move to target thread (%p)\n",currentData->thread.loadRelaxed(), thisThreadData->thread.loadRelaxed(), targetData ? targetData->thread.loadRelaxed() : nullptr);return;}// 移动前先发送QEvent::ThreadChange事件d->moveToThread_helper();if (!targetData)targetData = new QThreadData(0);//确保在我们移动这个对象时没有人添加/删除连接QMutexLocker l(signalSlotLock(this));QOrderedMutexLocker locker(¤tData->postEventList.mutex,&targetData->postEventList.mutex);// 保持currentData存活(因为我们已经锁定了它)currentData->ref();// 真正的移动对象d_func()->setThreadData_helper(currentData, targetData);locker.unlock();// now currentData can commit suicide if it wants tocurrentData->deref();
}void QObjectPrivate::moveToThread_helper()
{Q_Q(QObject);QEvent e(QEvent::ThreadChange);//sendEvent发送ThreadChange事件//因为sendEvent是同步调用,所以等对象处理完事件后,才能继续往下走QCoreApplication::sendEvent(q, &e);//给对象的所有孩子发送ThreadChange事件for (int i = 0; i < children.size(); ++i) {QObject *child = children.at(i);child->d_func()->moveToThread_helper();}
}void QObjectPrivate::setThreadData_helper(QThreadData *currentData, QThreadData *targetData)
{for (int i = 0; i < currentData->postEventList.size(); ++i) {const QPostEvent &pe = currentData->postEventList.at(i);if (!pe.event)continue;if (pe.receiver == q) {//将此post事件移动到targetList中targetData->postEventList.addEvent(pe);const_cast<QPostEvent &>(pe).event = nullptr;++eventsMoved;}}//当前线程在调用moveToThread()后不应该恢复currentSenderConnectionData *cd = connections.loadRelaxed();if (cd) {if (cd->currentSender) {cd->currentSender->receiverDeleted();cd->currentSender = nullptr;}// 调整连接中的receiverThreadId值if (cd) {auto *c = cd->senders;while (c) {QObject *r = c->receiver.loadRelaxed();if (r) {Q_ASSERT(r == q);targetData->ref();QThreadData *old = c->receiverThreadData.loadRelaxed();if (old)old->deref();c->receiverThreadData.storeRelaxed(targetData);}c = c->next;}}}// 设置新线程数据targetData->ref();threadData.loadRelaxed()->deref();//同步其孩子到targetData线程//QObject的孩子肯定不是widget一类,所以可以直接移动不用判断for (int i = 0; i < children.size(); ++i) {QObject *child = children.at(i);child->d_func()->setThreadData_helper(currentData, targetData);}
}
这篇关于QObject_thread的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!