Results 1 to 13 of 13

Thread: Why I cannot stop a thread?

  1. #1
    Join Date
    Nov 2012
    Posts
    232
    Thanks
    118
    Thanked 18 Times in 10 Posts
    Platforms
    Windows Android

    Default Why I cannot stop a thread?

    Hi,

    I cannot stop a thread.

    main.cpp
    Qt Code:
    1. #include <QCoreApplication>
    2. #include "mythread.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QCoreApplication a(argc, argv);
    7.  
    8. MyThread mThread1;
    9. mThread1.name = "mThread1";
    10.  
    11. mThread1.start();
    12.  
    13. mThread1.stop = true;
    14.  
    15. return a.exec();
    16. }
    To copy to clipboard, switch view to plain text mode 

    mythread.h
    Qt Code:
    1. #ifndef MYTHREAD_H
    2. #define MYTHREAD_H
    3.  
    4. #include <QtCore>
    5.  
    6. class MyThread : public QThread
    7. {
    8. public:
    9. MyThread();
    10. void run();
    11. QString name;
    12. bool stop;
    13. };
    14.  
    15. #endif // MYTHREAD_H
    To copy to clipboard, switch view to plain text mode 

    mythread.cpp
    Qt Code:
    1. #include "mythread.h"
    2. #include <QDebug>
    3.  
    4. MyThread::MyThread()
    5. {
    6. }
    7.  
    8. void MyThread::run() {
    9. qDebug() << this->name << " Running";
    10.  
    11. QMutex mutex;
    12. mutex.lock();
    13. this->stop = false;
    14. mutex.unlock();
    15.  
    16. for (int i = 0; i < 1000; i++) {
    17. QMutex mutex;
    18. mutex.lock();
    19. if (this->stop) {
    20. break;
    21. }
    22. mutex.unlock();
    23. this->sleep(1);
    24. qDebug() << this->name << " " << i;
    25. }
    26. }
    To copy to clipboard, switch view to plain text mode 

    It's the example from this video: http://www.youtube.com/watch?v=5WEiQ3VJfxc

    Thank you!

  2. #2
    Join Date
    Jan 2012
    Location
    Iran, Tehran
    Posts
    308
    Thanks
    75
    Thanked 24 Times in 21 Posts
    Qt products
    Qt4 Qt5 PyQt3 PyQt4
    Platforms
    Unix/X11 Windows

    Default Re: Why I cannot stop a thread?

    Write a stop() method in thread and call terminate method of thread

    void stop()
    {
    terminate();
    wait(500);
    }

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

    8Observer8 (26th August 2013)

  4. #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: Why I cannot stop a thread?

    Quote Originally Posted by 8Observer8 View Post
    Qt Code:
    1. QMutex mutex;
    2. mutex.lock();
    3. this->stop = false;
    4. mutex.unlock();
    5. for (int i = 0; i < 1000; i++) {
    6. QMutex mutex;
    7. mutex.lock();
    8. if (this->stop) {
    9. break;
    10. }
    11. mutex.unlock();
    12. this->sleep(1);
    13. qDebug() << this->name << " " << i;
    14. }
    15. }
    To copy to clipboard, switch view to plain text mode 
    This code makes no sense, by the way, especially use of the mutexes.
    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.


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

    8Observer8 (26th August 2013)

  6. #4
    Join Date
    Nov 2012
    Posts
    232
    Thanks
    118
    Thanked 18 Times in 10 Posts
    Platforms
    Windows Android

    Default Re: Why I cannot stop a thread?

    Quote Originally Posted by alizadeh91 View Post
    Write a stop() method in thread and call terminate method of thread

    void stop()
    {
    terminate();
    wait(500);
    }
    Yes, I will do it later. Why is "wait(500);" here?

  7. #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: Why I cannot stop a thread?

    If you "do that later" then "later" it might work and now it won't.
    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.


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

    8Observer8 (26th August 2013)

  9. #6
    Join Date
    Nov 2012
    Posts
    232
    Thanks
    118
    Thanked 18 Times in 10 Posts
    Platforms
    Windows Android

    Default Re: Why I cannot stop a thread?

    Why is it incorrect code?

    Qt Code:
    1. void MyThread::run () {
    2. QMutex mutex;
    3. mutex.lock();
    4. this->stop = false;
    5. mutex.unlock();
    6.  
    7. for (int i = 0; i < 1000; i++) {
    8. QMutex mutex;
    9. mutex.lock();
    10. if (this->stop) break;
    11. mutex.unlock();
    12.  
    13. this->msleep(100);
    14.  
    15. emit numberChanged(i);
    16. }
    17. }
    To copy to clipboard, switch view to plain text mode 

  10. #7
    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: Why I cannot stop a thread?

    Quote Originally Posted by 8Observer8 View Post
    Why is it incorrect code?
    Your mutexes are not protecting anything. For a mutex to work two or more threads have to access the same mutex.

    By the way, you're code can be rewritten to work without any mutex, without any for loop and without having to check any stop condition. And definitely without this ugly sleep call.
    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.


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

    8Observer8 (26th August 2013)

  12. #8
    Join Date
    Nov 2012
    Posts
    232
    Thanks
    118
    Thanked 18 Times in 10 Posts
    Platforms
    Windows Android

    Default Re: Why I cannot stop a thread?

    Thank you!

    Is it right?



    Thread_with_terminate.pro
    Qt Code:
    1. HEADERS += \
    2. threaddialog.h \
    3. mythread.h
    4.  
    5. SOURCES += \
    6. threaddialog.cpp \
    7. mythread.cpp \
    8. main.cpp
    To copy to clipboard, switch view to plain text mode 

    main.cpp
    Qt Code:
    1. #include "threaddialog.h"
    2. #include <QApplication>
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7.  
    8. ThreadDialog w;
    9. w.resize(100, 50);
    10. w.show();
    11.  
    12. return a.exec();
    13. }
    To copy to clipboard, switch view to plain text mode 

    mythread.h
    Qt Code:
    1.  
    To copy to clipboard, switch view to plain text mode 

    mythread.cpp
    Qt Code:
    1. #ifndef MYTHREAD_H
    2. #define MYTHREAD_H
    3.  
    4. #include <QtCore>
    5.  
    6. class MyThread : public QThread
    7. {
    8. Q_OBJECT
    9. public:
    10. explicit MyThread(QObject *parent = 0);
    11. void run();
    12. void stop();
    13.  
    14. signals:
    15. void numberChanged(int);
    16.  
    17. public slots:
    18.  
    19. };
    20.  
    21. #endif // MYTHREAD_H
    To copy to clipboard, switch view to plain text mode 

    threaddialog.h
    Qt Code:
    1. #ifndef THREADDIALOG_H
    2. #define THREADDIALOG_H
    3.  
    4. #include <QDialog>
    5. #include <QtGui>
    6. #include "mythread.h"
    7.  
    8. class ThreadDialog : public QDialog
    9. {
    10. Q_OBJECT
    11. public:
    12. explicit ThreadDialog(QWidget *parent = 0);
    13.  
    14. signals:
    15.  
    16. public slots:
    17. void numberChanged(int);
    18.  
    19. private slots:
    20. void on_startButton_clicked();
    21. void on_stopButton_clicked();
    22.  
    23. private:
    24. QLabel *numberLabel;
    25. MyThread *myThread;
    26. };
    27.  
    28. #endif // THREADDIALOG_H
    To copy to clipboard, switch view to plain text mode 

    threaddialog.cpp
    Qt Code:
    1. #include "threaddialog.h"
    2.  
    3. ThreadDialog::ThreadDialog(QWidget *parent) :
    4. QDialog(parent)
    5. {
    6. numberLabel = new QLabel(tr("Number"));
    7. QPushButton *startButton = new QPushButton(tr("Start"));
    8. QPushButton *stopButton = new QPushButton(tr("Stop"));
    9.  
    10. QHBoxLayout *mainLayout = new QHBoxLayout(this);
    11. mainLayout->addWidget(numberLabel);
    12. mainLayout->addStretch();
    13. mainLayout->addWidget(startButton);
    14. mainLayout->addWidget(stopButton);
    15.  
    16. connect(startButton, SIGNAL(clicked()), this, SLOT(on_startButton_clicked()));
    17. connect(stopButton, SIGNAL(clicked()), this, SLOT(on_stopButton_clicked()));
    18.  
    19. myThread = new MyThread(this);
    20. connect(myThread, SIGNAL(numberChanged(int)), this, SLOT(numberChanged(int)));
    21. }
    22.  
    23. void ThreadDialog::on_startButton_clicked()
    24. {
    25. myThread->start();
    26. }
    27.  
    28. void ThreadDialog::on_stopButton_clicked()
    29. {
    30. myThread->stop();
    31. }
    32.  
    33. void ThreadDialog::numberChanged(int number)
    34. {
    35. numberLabel->setText(QString::number(number));
    36. }
    To copy to clipboard, switch view to plain text mode 

  13. #9
    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: Why I cannot stop a thread?

    I don't see code for the thread anywhere but regardless, it's still likely not what I had in mind (based on what I see).
    Last edited by wysota; 27th August 2013 at 07:45.
    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.


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

    8Observer8 (27th August 2013)

  15. #10
    Join Date
    Nov 2012
    Posts
    232
    Thanks
    118
    Thanked 18 Times in 10 Posts
    Platforms
    Windows Android

    Default Re: Why I cannot stop a thread?

    Sorry!

    mythread.h
    Qt Code:
    1. #ifndef MYTHREAD_H
    2. #define MYTHREAD_H
    3.  
    4. #include <QtCore>
    5.  
    6. class MyThread : public QThread
    7. {
    8. Q_OBJECT
    9. public:
    10. explicit MyThread(QObject *parent = 0);
    11. void run();
    12. void stop();
    13.  
    14. signals:
    15. void numberChanged(int);
    16.  
    17. public slots:
    18.  
    19. };
    20.  
    21. #endif // MYTHREAD_H
    To copy to clipboard, switch view to plain text mode 

    mythread.cpp
    Qt Code:
    1. #include "mythread.h"
    2. #include <QtCore>
    3.  
    4. MyThread::MyThread(QObject *parent) :
    5. QThread(parent)
    6. {
    7. }
    8.  
    9. void MyThread::run () {
    10. for (int i = 0; i < 1000; i++) {
    11.  
    12. this->msleep(100);
    13.  
    14. emit numberChanged(i);
    15. }
    16. }
    17.  
    18. void MyThread::stop() {
    19. this->terminate();
    20. wait(500);
    21. }
    To copy to clipboard, switch view to plain text mode 

  16. #11
    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: Why I cannot stop a thread?

    Yeah... not what I had in mind.
    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.


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

    8Observer8 (27th August 2013)

  18. #12
    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: Why I cannot stop a thread?

    Quote Originally Posted by alizadeh91 View Post
    Write a stop() method in thread and call terminate method of thread

    void stop()
    {
    terminate();
    wait(500);
    }
    That is probably the worst possible suggestion ever given to someone wanting to stop a thread.
    Repeat after me: never ever call terminate() on a thread!

    The correct way is to indicate the wish to stop, e.g. by setting a stop flag, and then have the thread exit itself. Termination can leave mutexes locked, leak memory due to shutdown code not being executed, etc.
    If you call terminate() you should be aware that your program might crash or lock up.

    The original program's problem is that MyThread::run() resets the stop flag to false. Initialisation of the stop flag has to be done in MyThread's constructor.
    Obviously setting it from main without locking is also an issue, but not as grave as overwriting the value set by the main thread.

    So the actually recommendable solution to the problem is to
    1) initialize the stop flag in the constructor
    2) make the stop() method set the stop flag to true using mutex protection
    3) check the flag in run() using mutex protection.

    Cheers,
    _

  19. The following 3 users say thank you to anda_skoa for this useful post:

    8Observer8 (3rd September 2013), alizadeh91 (18th September 2013), Momergil (5th January 2014)

  20. #13
    Join Date
    Nov 2012
    Posts
    232
    Thanks
    118
    Thanked 18 Times in 10 Posts
    Platforms
    Windows Android

    Default Re: Why I cannot stop a thread?

    Thank you very much

Similar Threads

  1. Replies: 1
    Last Post: 25th October 2012, 15:10
  2. Replies: 0
    Last Post: 16th April 2012, 22:58
  3. Replies: 2
    Last Post: 2nd April 2012, 23:53
  4. Replies: 5
    Last Post: 22nd February 2011, 21:21
  5. Replies: 16
    Last Post: 7th October 2009, 08:17

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.