Results 1 to 6 of 6

Thread: Qthreads

  1. #1
    Join Date
    Jan 2007
    Posts
    21
    Thanks
    2
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Qthreads

    Hi...I am creating a thread using QThread..
    When i exit my application i want to kill this thread but i am getting the following error :

    QThread object destroyed while thread is still running.

    i want to kill this thread in the destructor of my main window...i tried calling terminate() in the destructor but i am still getting the same error...

    please let me know soon wht can be done..

  2. #2
    Join Date
    Jan 2006
    Location
    Kerala
    Posts
    371
    Thanks
    76
    Thanked 37 Times in 32 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qthreads

    use QThread::wait() to wait for threads to complete before exiting.
    We can't solve problems by using the same kind of thinking we used when we created them

  3. #3
    Join Date
    Jan 2007
    Posts
    21
    Thanks
    2
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Qthreads

    i am using the following where i want to kill the thread :

    MyThread.wait();
    MyThread.exit();

    What i have observed is that i still need to put a sleep() or a while loop between wait() and exit().
    If i dont use the same then i still get the same error.
    Is this the right way??

  4. #4
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qthreads

    When playing with Qthread I often use the following code in the destructor :
    Qt Code:
    1. while ( isRunning() ) quit();
    To copy to clipboard, switch view to plain text mode 
    Current Qt projects : QCodeEdit, RotiDeCode

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    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: Qthreads

    Quote Originally Posted by fullmetalcoder View Post
    When playing with Qthread I often use the following code in the destructor :
    Qt Code:
    1. while ( isRunning() ) quit();
    To copy to clipboard, switch view to plain text mode 
    Oh, this really sucks - your CPU will go 100% if you do this. Calling wait() (and maybe quit() before that if you're running an event loop in the worker thread) should be enough, just make sure worker threads have a chance to exit at all.

  6. #6
    Join Date
    Jan 2006
    Location
    Munich, Germany.
    Posts
    111
    Thanks
    29
    Thanked 3 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: Qthreads

    It might not apply to your job, but...

    if you need to break off your job (stopping the thread in the middle of it's work) you can use a 'bool keepRunning' member variable in the threaded function.

    run(){
    keepRunning = true;
    do{
    //whatever - some short job
    }while(someCondition&&keepRunning);

    Then sub-class quit to look like

    void AutomationThread::quit()
    {
    keepRunning = false;
    QThread::quit();
    }
    Then you can stop the thread with
    myThread->quit();
    myThread->wait();//this will wait a maximum of one loop iteration.

    Not always applicable, but I use that method all the time - my threads can run for hours (monitoring things, waiting)

    by the way, a long sleep in my thread then looks like
    for(int i=0; i<someLargeWait&&keepRunning; ++i){
    msleep(100);
    }
    which can also be broken off quickly.

    K

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

    Sheetal (29th March 2007)

Similar Threads

  1. QTcpSockets and QThreads
    By TheRonin in forum Newbie
    Replies: 3
    Last Post: 21st June 2006, 10:41

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.