I am doing an exercise qt console application on threading, here is the code:

Qt Code:
  1. // make two thread, one checking on the state of the other
  2.  
  3.  
  4. //////////////////////////////////////
  5. // main.cpp
  6.  
  7. #include "mytimer.h"
  8. #include "mythread.h"
  9. #include "checkthread.h"
  10. #include <QCoreApplication>
  11. #include <QString>
  12. #include <QFile>
  13. #include <QDebug>
  14.  
  15.  
  16. int main(int argc, char *argv[])
  17. {
  18. QCoreApplication a(argc, argv);
  19. MyThread mThread1;
  20. mThread1.name = "thread 1";
  21. mThread1.start(QThread::HighestPriority);
  22. CheckThread mCheck(&mThread1);
  23. mCheck.start();
  24.  
  25. return a.exec();
  26. }
  27.  
  28. ///////////////////////////////////////
  29. // mythread.h
  30.  
  31. #ifndef MYTHREAD_H
  32. #define MYTHREAD_H
  33.  
  34. #include <QThread>
  35. #include <QtCore>
  36.  
  37. class MyThread : public QThread
  38. {
  39. public:
  40. MyThread();
  41. void run();
  42. QString name;
  43. bool stop;
  44. int sum;
  45. };
  46.  
  47. #endif // MYTHREAD_H
  48.  
  49. //////////////////////////////////////
  50. // mythread.cpp
  51.  
  52. #include "mythread.h"
  53.  
  54. MyThread::MyThread()
  55. {
  56. sum = 0;
  57. }
  58.  
  59. void MyThread::run()
  60. {
  61. qDebug() << this->name << " running...";
  62. for(int i=0; i<1000; i++) {
  63. this->sum += i;
  64. qDebug() << this->name << " counting " << sum;
  65. this->sleep(1);
  66. if(this->stop) {
  67. break;
  68. }
  69. }
  70. }
  71.  
  72. //////////////////////////////////////
  73. // checkthread.h
  74.  
  75.  
  76. #ifndef CHECKTHREAD_H
  77. #define CHECKTHREAD_H
  78.  
  79. #include <QThread>
  80. #include "mythread.h"
  81.  
  82. class CheckThread : public QThread
  83. {
  84. Q_OBJECT
  85. public:
  86. explicit CheckThread(QObject *parent = 0);
  87. explicit CheckThread(MyThread *tocheck);
  88. void run();
  89. MyThread *tocheck_;
  90. };
  91.  
  92. #endif // CHECKTHREAD_H
  93.  
  94. //////////////////////////////////////
  95. // checkthread.cpp
  96.  
  97.  
  98. #include "checkthread.h"
  99.  
  100. CheckThread::CheckThread(QObject *parent) :
  101. QThread(parent)
  102. {
  103. }
  104.  
  105. CheckThread::CheckThread(MyThread *tocheck) :
  106. tocheck_(tocheck)
  107. {
  108. }
  109.  
  110. void CheckThread::run() {
  111. while(true) {
  112. this->sleep(1);
  113. if(tocheck_->sum > 15) {
  114. tocheck_->stop = true;
  115. }
  116. }
  117. }
To copy to clipboard, switch view to plain text mode 

The expected behavior is that mThread1 shoud count to 15 and then stop,
but instead it is stuck at 0.

Interestingly (strangely, i mean), if I add the following code into the main.cpp file, then it runs
ok:


Qt Code:
  1. void Write(QString Filename)
  2. {
  3. QFile fh(Filename);
  4. if(!fh.open(QFile::WriteOnly | QFile::Text))
  5. {
  6. qDebug() << "Could not open file for writing";
  7. return;
  8. }
  9. QTextStream out(&fh);
  10. out << "hi world";
  11. fh.flush();
  12. fh.close();
  13. }
  14.  
  15. void Read(QString Filename)
  16. {
  17. QFile fh(Filename);
  18. if(!fh.open(QFile::ReadOnly | QFile::Text))
  19. {
  20. qDebug() << "Could not open file for writing";
  21. return;
  22. }
  23. QTextStream in(&fh);
  24. QString mtext = in.readAll();
  25. qDebug() << mtext;
  26. }
To copy to clipboard, switch view to plain text mode 

I am using qt 4.8 on a kubuntu 13.10 machine, and the ide is qt creator 3.0.1