deleteLater()是否等待所有待处理的信号被传递? [英] Does deleteLater() wait for all pending signals to be delivered?

查看:212
本文介绍了deleteLater()是否等待所有待处理的信号被传递?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在想着写这段代码:

  emit finished(); 
deleteLater();但是它让我想知道 finished() >将始终在删除对象之前传递。我很确定它会交付 Qt :: DirectConnection ,但我不确定 Qt :: QueuedConnection ,或者如果该槽位于另一个线程中。

解决方案

发出信号时,的接收对象的线程进行处理。调用deleteLater还会将一个事件添加到队列中,因此如果接收对象和正在删除的对象具有相同的线程亲缘关系,则无论线程类型如何,都将按顺序执行。



如果发送方和接收方具有不同的线程亲和性(在不同的线程中运行),那么我期望在发射完成之前调用deleteLater是可能的,如果接收方的事件循环在发送方的事件循环之前开始处理。



如果您想要保证完成操作首先被执行,您可以使用阻止连接来连接发件人和接收者,这将阻止发件人的线程,直到邮件被递送

  connect(sender,SIGNAL(finished()),receiver,SLOT(handleFinished(),Qt :: BlockingQueuedConnection);请注意,如果使用Qt :: BlockingQueuedConnection并且发送方和接收方具有相同的线程亲和性,则应用程序可以使用Qt :: BlockingQueuedConnection将被锁定。


I was thinking about writing this code:

emit finished();
deleteLater();

But it made me wonder if finished() would always be delivered before the object is deleted. I'm pretty certain it will be delivered for Qt::DirectConnection, but I'm not sure about Qt::QueuedConnection, or if the slot is in another thread.

解决方案

When you emit a signal, it is placed in the event queue of the thread of the receiving object for processing. Calling deleteLater also adds an event to the queue, so they will be performed sequentially if the receiving object and the object being deleted have the same thread affinity, regardless of the thread type.

If the sender and receiver have different thread affinity (running in different threads) then I would expect it is possible for deleteLater to be called before the emit is finished, if the receiver's event loop starts processing before the sender's event loop.

If you want to guarantee that finished is executed first, you can connect the sender and receiver with a blocking connection, which will halt the sender's thread until the message has been delivered.

connect(sender, SIGNAL(finished()), receiver, SLOT(handleFinished(), Qt::BlockingQueuedConnection);

Note that if using Qt::BlockingQueuedConnection and the sender and receiver have the same thread affinity, the application will be dead-locked.

这篇关于deleteLater()是否等待所有待处理的信号被传递?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆