Results 1 to 8 of 8

Thread: when can I delete a thread?

  1. #1
    Join Date
    May 2009
    Posts
    75
    Thanks
    5
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default when can I delete a thread?

    In a project I use Qthreads. Some actions are triggered by clicking a button, and are executed from a thread (to lete interfac be responsive during actions executions)

    I have attached a little example to figure out my doubt
    ThreadTest.zip

    I have this dialog:
    ThreadExample.jpg

    When the button is clicked is called the following slot:
    Qt Code:
    1. void Dialog::slotBtn(void)
    2. {
    3. // MyThread *myt; // declared in the class
    4. myt = new MyThread;
    5.  
    6. myt->start();
    7. }
    To copy to clipboard, switch view to plain text mode 

    where MyThread is:
    Qt Code:
    1. #include <QThread>
    2.  
    3. class MyThread : public QThread
    4. {
    5. public:
    6. MyThread();
    7.  
    8. void run(void);
    9. };
    10.  
    11. MyThread::MyThread()
    12. {
    13. }
    14.  
    15.  
    16. void MyThread::run(void)
    17. {
    18. // some stuff that can takes some time
    19. sleep(1);
    20.  
    21. qWarning("hallo world from MyThread");
    22. //end of thread
    23. }
    To copy to clipboard, switch view to plain text mode 

    I wonder when to delete myt.

    For example, when a thread finish a signal QThread::finished() is emitted. I can connect to a slot, but I wonder if it is safe to delete it inside that slot.

    thanks

  2. #2
    Join Date
    Dec 2008
    Location
    Poland
    Posts
    383
    Thanks
    52
    Thanked 42 Times in 42 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: when can I delete a thread?

    The way I would do that is:

    Qt Code:
    1. void Dialog::slotBtn(void)
    2. {
    3. // MyThread *myt; // declared in the class - on stack
    4. myt = new MyThread;
    5. connect( myt, SIGNAL(finished()), this, SLOT(mytFinishedSlot()));
    6. button->setEnabled(false); //to prevent running again the same thread
    7. myt->start();
    8. }
    9.  
    10. void Dialog::mytFinishedSlot(){
    11. //retrieve the data from the thread
    12. disconnect( myt, SIGNAL(finished()), this, SLOT(mytFinishedSlot()));
    13. delete myt;
    14. button->setEnabled(true); //"turn one" button
    15. }
    To copy to clipboard, switch view to plain text mode 
    Comments and critique are more then welcome.
    In the near future - corporate networks reach out to the stars. Electrons and light flow throughout the universe.
    The advance of computerization however, has not yet wiped out nations and ethnic groups.

  3. #3
    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: when can I delete a thread?

    Quote Originally Posted by Talei View Post
    Comments and critique are more then welcome.
    It would crash your application. You can't delete an object from within a slot connected to a signal emitted by the same object. But you can use deleteLater(). And you don't have to disconnect the signal/slot connection - it will be done automatically when one of the objects involved in the connection is destroyed.
    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.


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

    Talei (12th September 2010)

  5. #4
    Join Date
    Dec 2008
    Location
    Poland
    Posts
    383
    Thanks
    52
    Thanked 42 Times in 42 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: when can I delete a thread?

    Simple example and two errors... eh.
    I tested this code and It didn't crash application, so I'm a little bit confused about that.
    Please correct me, as I'm self taught and can misunderstand some aspect of signal/slot mechanism.
    I use disconnect to ensure that object is not connected to anything before delete, and at that point object should have no connection, and then simply delete the object, so the slot is not wired to anything and AFAIK should be treat as function (this is how I understand it from the docs, so probably I understand it wrong).

    Also I just look at the qcoreapplication.cpp post event (because logically there must be the difference between delete and deletelater()), that is executed with deletelater() and from what I can understand:
    when I use delete object, I delete only the object and event are still in mem.?
    when deletelater() is used, I delete not only the object but also all the events associated with this object?
    Or I'm completely mistaken with this?
    Last edited by Talei; 11th September 2010 at 00:57. Reason: spelling corrections
    In the near future - corporate networks reach out to the stars. Electrons and light flow throughout the universe.
    The advance of computerization however, has not yet wiped out nations and ethnic groups.

  6. #5
    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: when can I delete a thread?

    Quote Originally Posted by Talei View Post
    I tested this code and It didn't crash application, so I'm a little bit confused about that.
    Because it depends on the context. Notice that there may be another slot connected to the same signal and if your slot is ran before the other one, the crash is bound to occur.

    I use disconnect to ensure that object is not connected to anything before delete, and at that point object should have no connection, and then simply delete the object, so the slot is not wired to anything and AFAIK should be treat as function (this is how I understand it from the docs, so probably I understand it wrong).
    A slot is a regular method and you don't have to do anything for it to be treated this way. If you are in a context of a function that will delete the object, no more signals will be delivered to the object and the method will not be called as a slot again. But this is irrelevant. What is relevant is that the destructor of QObject will remove all the connections itself.

    when I use delete object, I delete only the object and event are still in mem.?
    I don't know what you mean by that. I don't know if it answers your question but deleteLater() is a so called "deferred deletion" which will be deferred furhter if there are events pending for the object in the event queue so all pending events will be delivered prior to the actual destruction of the object. In case of a simple delete all pending events will be removed from the queue (if not immediately then during the next sweep of the event queue).
    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.


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

    Talei (12th September 2010)

  8. #6
    Join Date
    Dec 2008
    Location
    Poland
    Posts
    383
    Thanks
    52
    Thanked 42 Times in 42 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: when can I delete a thread?

    What confuses me about QThread and delete from within slot is that, finished() is emited after thread event loop stops(I assume that after this signal it is guaranteed that qthread event loop stops immediately - please read * ). At this point execution of thread ended and there is no pending event's inside QThread (in case presented above, of course there can be some pending user event's that needs to be dealt with ). So I assumed that I can delete that QThread object because there is no pending events (to ensure that this is true I manually disconnect object ).

    *Or when finished() is emited event loop, depending on the i.e. CPU load, is still running, but it's event loop is queued to stop, so deleting qthread within finished() slot will actually crash app, because event loop of qthread didn't stooped yet,of course this is if'y situation depending on the various factors like cpu load, app context, etc..

    So as far as I understand correct way of deleting qobject, in this case qthread, is to:
    Qt Code:
    1. void Dialog::slotBtn(void)
    2. {
    3. // MyThread *myt; // declared in the class - on stack
    4. myt = new MyThread;
    5. connect( myt, SIGNAL(finished()), this, SLOT(mytFinishedSlot()));
    6. button->setEnabled(false); //to prevent running again the same thread
    7. myt->start();
    8. }
    9.  
    10. void Dialog::mytFinishedSlot(){
    11. //retrieve the data from the thread
    12. myt.deleteLater();
    13. button->setEnabled(true); //"turn one" button
    14. }
    To copy to clipboard, switch view to plain text mode 
    In the near future - corporate networks reach out to the stars. Electrons and light flow throughout the universe.
    The advance of computerization however, has not yet wiped out nations and ethnic groups.

  9. #7
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: when can I delete a thread?

    Quote Originally Posted by Talei View Post
    What confuses me about QThread and delete from within slot is that, finished() is emited after thread event loop stops(I assume that after this signal it is guaranteed that qthread event loop stops immediately - please read * ). At this point execution of thread ended and there is no pending event's inside QThread (in case presented above, of course there can be some pending user event's that needs to be dealt with ). So I assumed that I can delete that QThread object because there is no pending events (to ensure that this is true I manually disconnect object ).
    Ahh this is the classical QThread mistake. A QThread object lives in the main thread. It makes use of the main thread event loop.
    However, QThread launches a new thread with its own eventloop.

    Qt Code:
    1. +---------------------------------+
    2. | |
    3. | Lives in the main thread. |
    4. | Uses the main thread event loop |
    5. | |
    6. | +-----------------------------+ |
    7. | | Thread | |
    8. | | Has own eventloop | |
    9. | +-----------------------------+ |
    10. | |
    11. +---------------------------------+
    To copy to clipboard, switch view to plain text mode 

    When a thread is finished, it doesn't mean the QThread object can't or will not receive events from the main thread. There are some perfectly sound examples where your thread can emit the finished signal and the QThread object can receive a new event or signal to restart the thread again.

    Edit:

    no pending events (to ensure that this is true I manually disconnect object)
    And: disconnecting an object doesn't mean the object can not receive events.
    Last edited by tbscope; 12th September 2010 at 07:34.

  10. The following user says thank you to tbscope for this useful post:

    Talei (12th September 2010)

  11. #8
    Join Date
    Dec 2008
    Location
    Poland
    Posts
    383
    Thanks
    52
    Thanked 42 Times in 42 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: when can I delete a thread?

    I read so much about QThread, and I knew that QThread is not actual thread but still made such a mistake... eh there is a long way to go for me .
    And when I looked at Your chart it came back to me immediately , and right now I got a bigger picture of QThread.

    Thanks you guys for wasting Your valuable time to answering my questions.
    In the near future - corporate networks reach out to the stars. Electrons and light flow throughout the universe.
    The advance of computerization however, has not yet wiped out nations and ethnic groups.

Similar Threads

  1. Replies: 4
    Last Post: 19th February 2009, 11:10
  2. delete my thread
    By Dmitry in forum Qt Programming
    Replies: 1
    Last Post: 15th October 2006, 17:42

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.