Results 1 to 4 of 4

Thread: Make one qthread check the state of the other

  1. #1
    Join Date
    May 2014
    Posts
    2
    Qt products
    Qt4 Qt5 PyQt3 PyQt4
    Platforms
    Unix/X11

    Default Make one qthread check the state of the other

    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

  2. #2
    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: Make one qthread check the state of the other

    You don't initialize MyThread::stop.
    If my chance it is true, the thread will exit in the first iteration of its loop.

    Also for a correct program you would need to protect the access of variables that are used by more than one thread. e.g. using QMutex

    Cheers,
    _

  3. #3
    Join Date
    May 2014
    Posts
    2
    Qt products
    Qt4 Qt5 PyQt3 PyQt4
    Platforms
    Unix/X11

    Default Re: Make one qthread check the state of the other

    Yes, you are right!

    But this does not explain why the Read and Write functions in main.cpp solves the problem. Very puzzling.

  4. #4
    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: Make one qthread check the state of the other

    Quote Originally Posted by kindlychung View Post
    But this does not explain why the Read and Write functions in main.cpp solves the problem. Very puzzling.
    Coincidence.

    Cheers,
    _

Similar Threads

  1. Replies: 5
    Last Post: 27th December 2013, 15:27
  2. QAction without check state
    By pardas in forum Newbie
    Replies: 2
    Last Post: 5th August 2012, 11:53
  3. QAbstractItemView and tri state check boxes
    By GrahamLabdon in forum Newbie
    Replies: 1
    Last Post: 3rd June 2011, 07:54
  4. Accessing check state of CheckBox in QTableWidget
    By lnxusr in forum Qt Programming
    Replies: 6
    Last Post: 22nd November 2009, 01:13
  5. Help: How to save Check box state
    By Garibalde in forum Qt Programming
    Replies: 4
    Last Post: 1st July 2009, 20:24

Tags for this Thread

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.