Results 1 to 9 of 9

Thread: QThread: Destroyed while thread is still running

  1. #1
    Join Date
    Dec 2006
    Posts
    103
    Thanks
    31
    Qt products
    Qt4
    Platforms
    Windows

    Default QThread: Destroyed while thread is still running

    hi all,
    i have problem on windows and linux too...
    i have 2 threads consumer and producer.
    but when i stop them or exit the application i get the following comments after execution:
    Running...
    QThread: Destroyed while thread is still running

    QThread: Destroyed while thread is still running

    QWaitCondition: Destroyed while threads are still waiting

    ---------------------- Exited normally ----------------------


    this problem has already been discussed on the following thread but i cudnt find the solution of my prob acc to the solution given in this :
    http://www.qtcentre.org/forum/f-qt-p...eads-5969.html

    Some code as :
    Qt Code:
    1. Consumer.cpp
    2. void Consumer::run()
    3. {
    4. j=0;
    5. while(!flag_stop)
    6. {
    7. usedBytes.acquire();
    8. write_str += buffer[j % 8096];
    9. j++;
    10. freeBytes.release();
    11. emit disp(); // disp a slot of mainwindow.cpp
    12. }
    13. flag_stop = true;
    14. }
    15. Producer.cpp
    16. void Producer::run()
    17. {
    18. i=0;
    19. while(!flag_stop)
    20. {
    21. if(flag_read)
    22. {
    23. freeBytes.acquire();
    24. int n = read_str.size();
    25. buffer[i % 8096] = (*((read_str.toUtf8().constData())+(n-1)));
    26. i++;
    27. usedBytes.release();
    28. flag_read = false;
    29. }
    30.  
    31. }
    32. flag_stop = true;
    33. }
    34.  
    35. Mainwindow.cpp
    36. ...
    37. connect(textEdit_1,SIGNAL(textChanged()),this,SLOT(read_send()));
    38. connect(pushButton_14,SIGNAL(clicked()),this,SLOT(Start_Thread()));
    39. connect(pushButton_15,SIGNAL(clicked()),this,SLOT(Stop_Thread()));
    40. consumer = new Consumer();
    41. producer = new Producer();
    42. connect(consumer,SIGNAL(disp()),this,SLOT(receive_write()));
    43. ...
    44. void MainWindowImpl::Start_Thread()
    45. {
    46. producer->start();
    47. consumer->start();
    48. }
    49. void MainWindowImpl::Stop_Thread()
    50. {
    51. flag_stop = true;
    52. }
    53. void MainWindowImpl::read_send()
    54. {
    55. read_str = textEdit_1->toPlainText();
    56. flag_read = true;
    57. }
    58. void MainWindowImpl::receive_write()
    59. {
    60. textEdit_2->setText(write_str);
    61. }
    62. void MainWindowImpl::closeEvent(QCloseEvent *event)
    63. {
    64. flag_stop = true;
    65. event->accept();
    66. }
    To copy to clipboard, switch view to plain text mode 
    Can any1 please help me that where i am misleading and where i have taken wrong approach.
    Thanks All...
    I worked on windows Xp with Qt 4.2.2(Open Source Version) and MinGw
    now i am trying the same things on Fedora Core 5 (linux-gcc) and Qt 4.2.2 open source edition.

  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: QThread: Destroyed while thread is still running

    You should call QThread::wait() on both threads from within your main thread to make sure they stopped (in Stop_Thread).

  3. #3
    Join Date
    Dec 2006
    Posts
    103
    Thanks
    31
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QThread: Destroyed while thread is still running

    when i do
    void MainWindowImpl::Stop_Thread()
    {
    flag_stop = true;
    producer->wait();
    consumer->wait();
    }
    then, if i dont press "Stop Thread" botton then i get
    "Running...
    QWaitCondition: Destroyed while threads are still waiting"
    and is i press stop thread button t hen the program doesnt respond.

    Globalvar.h
    #ifndef __GLOBALVAR_H__
    #define __GLOBALVAR_H__
    #include <QSemaphore>
    #include <QString>
    extern QSemaphore freeBytes;
    extern QSemaphore usedBytes;
    extern QString read_str, write_str;
    extern bool flag_read,flag_write,flag_stop;
    extern char buffer[8096];
    #endif // __GLOBALVAR_H__

    Globalvar.cpp
    #include "globalvar.h"
    char buffer[8096];
    QSemaphore freeBytes(8096);
    QSemaphore usedBytes;
    QString read_str, write_str;
    bool flag_read = false ,flag_write = false, flag_stop = false;

    So can u plz help me out?
    Thanks.
    I worked on windows Xp with Qt 4.2.2(Open Source Version) and MinGw
    now i am trying the same things on Fedora Core 5 (linux-gcc) and Qt 4.2.2 open source edition.

  4. #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: QThread: Destroyed while thread is still running

    Your application hangs because the threads are sleeping on their semaphores. You should first make sure the "flag_stop" will really stop them without making one of the threads sleep on its semaphore because of an empty/full buffer - if the "flag_stop" is set when the thread is already sleeping, it might never be woken up.

    The bottom line is, you have to go through your synchronisation mechanism again and rethink/correct it.

    For example this could work:
    Qt Code:
    1. void MainWindowImpl::Stop_Thread()
    2. {
    3. flag_stop = true;
    4. producer->release();
    5. consumer->release();
    6. producer->wait();
    7. consumer->wait();
    8. }
    9.  
    10. void Producer::run()
    11. {
    12. i=0;
    13. while(!flag_stop)
    14. {
    15. if(flag_read)
    16. {
    17. freeBytes.acquire();
    18. if(flag_stop) return;
    19. int n = read_str.size();
    20. buffer[i % 8096] = (*((read_str.toUtf8().constData())+(n-1)));
    21. i++;
    22. usedBytes.release();
    23. flag_read = false;
    24. }
    25.  
    26. }
    27. flag_stop = true;
    28. }
    29.  
    30. void Consumer::run()
    31. {
    32. j=0;
    33. while(!flag_stop)
    34. {
    35. usedBytes.acquire();
    36. if(flag_stop) return;
    37. write_str += buffer[j % 8096];
    38. j++;
    39. freeBytes.release();
    40. emit disp(); // disp a slot of mainwindow.cpp
    41. }
    42. flag_stop = true;
    43. }
    To copy to clipboard, switch view to plain text mode 

    As for the other problem - make sure you call Stop_Thread() before closing the application.

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

    Shuchi Agrawal (23rd March 2007)

  6. #5
    Join Date
    Dec 2006
    Posts
    103
    Thanks
    31
    Qt products
    Qt4
    Platforms
    Windows

    Question QThread: Destroyed while thread is still running

    Hi all,
    i am still having the same problem. i am not that good in programming and thus i am unable to fix this small problem. Please i wil be greatful to all who can help me out.
    I am not able to stop the threads. How to do it. The code is given in the .zip file.
    Threading.zip
    Thanks all.
    I worked on windows Xp with Qt 4.2.2(Open Source Version) and MinGw
    now i am trying the same things on Fedora Core 5 (linux-gcc) and Qt 4.2.2 open source edition.

  7. #6
    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: QThread: Destroyed while thread is still running

    I guess you didn't try my solution...

  8. #7
    Join Date
    Dec 2006
    Posts
    103
    Thanks
    31
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QThread: Destroyed while thread is still running

    Quote Originally Posted by wysota View Post
    I guess you didn't try my solution...
    yes i do tried solution given by you, but the entire program hangs on clicking "stop thread" button.
    Qt Code:
    1. void MainWindowImpl::Stop_Thread()
    2. { flag_stop = true;
    3. freeBytes.release(); //QSemaphore freeBytes;
    4. usedBytes.release(); //QSemaphore usedBytes;
    5. producer->wait();
    6. consumer->wait();
    7. }
    To copy to clipboard, switch view to plain text mode 
    So can u tel me where i m doing wrong.i wil be greatful beause i am reallly stuck here.
    I worked on windows Xp with Qt 4.2.2(Open Source Version) and MinGw
    now i am trying the same things on Fedora Core 5 (linux-gcc) and Qt 4.2.2 open source edition.

  9. #8
    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: QThread: Destroyed while thread is still running

    This is not what I suggested. I suggested checking the stop flag after going through the acquire call. The thing you do in your code snippet you pasted here does nothing. Many things can happen between release() and wait().

  10. #9
    Join Date
    Dec 2006
    Posts
    103
    Thanks
    31
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QThread: Destroyed while thread is still running

    Thanks a lot Wysota. i solved the problem by taking the approach "Your application hangs because the threads are sleeping on their semaphores. You should first make sure the "flag_stop" will really stop them without making one of the threads sleep on its semaphore because of an empty/full buffer - if the "flag_stop" is set when the thread is already sleeping, it might never be woken up." - quoted by you. Thanks a lot
    I worked on windows Xp with Qt 4.2.2(Open Source Version) and MinGw
    now i am trying the same things on Fedora Core 5 (linux-gcc) and Qt 4.2.2 open source edition.

Similar Threads

  1. How can I get the thread ID out of QThread
    By Artschi in forum Qt Programming
    Replies: 9
    Last Post: 8th November 2017, 04:27
  2. QThread, QMessageBox, gui thread
    By TheKedge in forum Qt Programming
    Replies: 1
    Last Post: 25th October 2006, 13:23
  3. how isRunning() finds thread is running?
    By quickNitin in forum Newbie
    Replies: 1
    Last Post: 13th June 2006, 09:03
  4. [QT4] QThread and printing a QList<QPixmap>
    By KShots in forum Qt Programming
    Replies: 3
    Last Post: 24th April 2006, 22:44
  5. Workload in a QThread blocks main application's event loop ?
    By 0xBulbizarre in forum Qt Programming
    Replies: 14
    Last Post: 9th April 2006, 22: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.