QObject :: deleteLater via QThread

I am looking for a solution for scheduling object deletion by thread. The documents on how they deleteLaterbehave are not entirely clear. Is it possible to call this function in a thread that does not own the object?

For example, Object X belongs to Thread A, and in Thread B I would like to delete Object X. Since the object may be inside the event processing at the moment (in Thread A), I cannot safely delete it until it returns to the message loop. If I call deleteLaterfrom Thread B, however the docs seem to indicate that it will delete as soon as Thread B returns to the message loop.

I am currently taking the approach to the signal emitted in stream A, which is attached to the slot that triggers deleteLater. I am wondering if there is an easier way to do this - if really, I can just call deleteLaterfrom any thread.

+8
source share
3 answers

Looking at the Qt 4 code and the Qt 5 code , it deleteLater()simply calls QCoreApplication::postEvent(), which is explicitly declared thread-oriented. Thus, it should be nice to just call directly. Since the event queue is processed in the thread of the object owner, deletion will occur in thread A.

Qt 5 QObject deleteLater() . Qt 4, postEvent().

+10

deleteLater() , object threadA -:

metaObject()->invokeMethod(object, "deleteLater", Qt::QueuedConnection);

.

+7

deleteLater() , , / int (.. ThreadB).

, ObjectX ThreadB, delete.

ThreadA, .

ObjectX , quit() ThreadA .

0

All Articles