Results 1 to 4 of 4

Thread: deleteLater called from different thread (std::thread)

  1. #1
    Join Date
    Jun 2012
    Posts
    58
    Thanks
    13
    Qt products
    Qt4

    Default deleteLater called from different thread (std::thread)

    I have a tree structure (say tree-A) the nodes of which are derived from QObject and std::enable_shared_from_this and std::shared_ptr is used to manage the lifetimes instead of QObject-parent-child. This is because there is a similar tree structure (tree-B) in an API which is non-qt and nodes in that tree give async updates to the corresponding connected nodes of tree-A. API is running in a separate thread (actually a threadpool). Nodes in tree-A have call back member functions connected to tree-B nodes using the shared_from_this() idiom (ie weak_ptr to shared_ptr conversions) to manage life-times. These callback functions further emit signals to finally bring the control to the slots in GUI thread. So it is quite possible in this shared ownership that API thread ends up deleting a tree-A node which would lead to undefined behaviour if the node was executing some slot. So to salvage this I’m using a custom deleter which would call deleteLater on the owned QObject derivative. However I am confused reading a few answers on the Net whether deleteLater() can be called from a different thread for a QObject created in another thread.
    So if API thread is the one that is going to destroy the QObject of some node of tree-A, is it to be expected that the call to deleteLater() on the QObject from the API thread will actually post the deletion request to the GUI thread (where the QObject was created)? Or do I have to invoke deleteLater() as a Qt::QueuedConnection slot to realize that?

    {Qt5.1.0, c++11}

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: deleteLater called from different thread (std::thread)

    deleteLater() post an event into the object thread's event loop using QCoreApplication:ostEvent(). That method is thread safe.

    Cheers,
    _

  3. #3
    Join Date
    Jun 2012
    Posts
    58
    Thanks
    13
    Qt products
    Qt4

    Default Re: deleteLater called from different thread (std::thread)

    oh, has anything changed or am i interpreting the documentation wrong? It says:
    ...for the object to be deleted, the control must return to the event loop from which deleteLater() was called.
    the thread from which deleteLater() was called (API thread) is not the same as thread in which QObject was created (GUI thread). So will deleteLater still post to the GUI thread?

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: deleteLater called from different thread (std::thread)

    Maybe that is just an unfortunate phrasing, wanting to say that the thread executing the object's event processing must return to event processing before anything happens.

    I am pretty sure that QCoreApplication:ostEvent() puts the event in the target object's event loop, anything else wouldn't make sense.
    A quick check of the code confirms that :-)

    Cheers,
    _

  5. The following user says thank you to anda_skoa for this useful post:

    ustulation (24th November 2013)

Similar Threads

  1. Qt problem in postEvent,called from thread.
    By Raadush in forum Qt Programming
    Replies: 1
    Last Post: 23rd November 2012, 10:32
  2. Replies: 1
    Last Post: 25th October 2012, 15:10
  3. slot in worker thread is not called
    By phenoboy in forum Qt Programming
    Replies: 1
    Last Post: 2nd June 2012, 19:45
  4. Slot not getting called, Qt::ConnectionType, Thread
    By StarShaper in forum Qt Programming
    Replies: 3
    Last Post: 7th February 2012, 14:04
  5. Replies: 9
    Last Post: 28th November 2009, 20:31

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.