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!