Results 1 to 4 of 4

Thread: Destruction in separate threads

  1. #1
    Join Date
    Mar 2012
    Posts
    2
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Question Destruction in separate threads

    Question 1:

    Let’s assume we have two threads (thread1 and thread2), each running their own event loop hosting a single QObject (obj1 and obj2). The signals of the two QObjects are connected to slots on the alternate object, and aside from this signal-slot communication, the two objects do not access each other directly.

    Is it safe if obj2 is destroyed, without disconnecting it from obj1 first, while obj1 continues to execute in its own thread?
    Is there any way that obj1 might hit some race condition where it sends a signal to obj2 before obj2 can disconnect itself?

    Keep in mind: In my hypothetical case, the thread2 message loop is being shut down immediately following the obj2 destruction, so any lingering obj2 messages shouldn’t be a problem (otherwise, I would use deleteLater()).

    Question 2:

    If a deleteLater signal is put into the message loop, but the message loop is then immediately exit()ed, is the object ever deleted? If it isn’t, is this a bug? It seems that deleteLater’s should always be processed, even if a thread is closing.

    I'm a bit new when it comes to QT, so I hope these questions aren't too fundamental. Googling only provides similar information, but nothing confirming either of these two issues. Thanks in advance

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Destruction in separate threads

    Quote Originally Posted by KevinKnowles View Post
    Is it safe if obj2 is destroyed, without disconnecting it from obj1 first, while obj1 continues to execute in its own thread?
    Yes.
    Is there any way that obj1 might hit some race condition where it sends a signal to obj2 before obj2 can disconnect itself?
    No.

    If a deleteLater signal is put into the message loop, but the message loop is then immediately exit()ed, is the object ever deleted?
    Define "immediately".
    If it isn’t, is this a bug?
    No, deleteLater() needs an event loop.

    It seems that deleteLater’s should always be processed, even if a thread is closing.
    If the thread exits you should first push the object to an existing thread to be sure its events can still be processed after its thread dies.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. The following user says thank you to wysota for this useful post:

    KevinKnowles (19th March 2012)

  4. #3
    Join Date
    Mar 2012
    Posts
    2
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Destruction in separate threads

    Quote Originally Posted by wysota View Post
    Define "immediately".
    I was thinking about this a bit more, and realized that I had overlooked the parent child relationships. Even if a deleteLater() gets thrown away because the thread is ending, as long as the threads "parent" objects are deleted, they will cascade down and clean up those children on the way out.

    I had somehow convinced myself that these "deleteLater" objects would never actually be deleted.

    Finally, order is restored to the wonderful world of unmanaged memory

    -- Thanks again

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Destruction in separate threads

    Quote Originally Posted by KevinKnowles View Post
    Even if a deleteLater() gets thrown away because the thread is ending, as long as the threads "parent" objects are deleted, they will cascade down and clean up those children on the way out.
    Objects living in thread A can't be children of QThread object representing thread A. Thus deleting the thread object will not delete objects used by the thread.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Problem with QProcess in two separate threads
    By viheaho in forum Qt Programming
    Replies: 2
    Last Post: 18th March 2010, 23:52
  2. Replies: 1
    Last Post: 7th December 2009, 08:26
  3. destruction of QGraphicsItem
    By killkolor in forum Qt Programming
    Replies: 2
    Last Post: 5th December 2009, 11:31
  4. What happens after closing and before destruction?
    By Raccoon29 in forum Qt Programming
    Replies: 45
    Last Post: 20th May 2008, 10:33
  5. Calling same method from separate threads
    By steg90 in forum Qt Programming
    Replies: 2
    Last Post: 19th July 2007, 09:55

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.