Results 1 to 2 of 2

Thread: Why is this thread not running?

  1. #1
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Thanks
    101
    Thanked 15 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Why is this thread not running?

    Hi there. Below is the source of a simple example where I start a thread that spins in an infinite loop, and watches if a global variable frameIdx has changed its value. I start the thread in the main function, wait a bit, and then increment the frameIdx in the main function and the thread has to notice that the value has changed. Like so:

    Qt Code:
    1. #include <QtConcurrent/QtConcurrent>
    2. #include <QDebug>
    3.  
    4. int frameIdx = 0; // globally accessible variable
    5.  
    6. void work()
    7. {
    8. qDebug() << "Worker thread started.";
    9.  
    10. int idx = 0;
    11. while(true)
    12. {
    13. qDebug() << "Thread" << counter << "frame idx" << idx << frameIdx; // This line makes the difference.
    14.  
    15. if (frameIdx > idx) // frameIdx has changed
    16. {
    17. qDebug() << "THREAD: increment frame idx" << idx << frameIdx;
    18. idx = frameIdx;
    19. }
    20. }
    21. }
    22.  
    23.  
    24. int main(int argc, char *argv[])
    25. {
    26. qDebug() << "Starting thread";
    27. QFuture<void> result = QtConcurrent::run(&work);
    28.  
    29. QThread::sleep(1); // Wait a little.
    30.  
    31. frameIdx++;
    32. qDebug() << "MAIN: frame idx incremented" << frameIdx;
    33.  
    34. return 0;
    35. }
    To copy to clipboard, switch view to plain text mode 


    This works as long as the qDebug() call in line 13 is enabled. Then, the thread writes something into the console in every iteration and eventually it tells me that the global variable has been incremented. If I comment the qDebug() line out, the thread is still started, I can see the "Worker thread started." message in the console, but the increment of the frameIdx is never reported. It seems that the worker thread is not running after all. Can anyone please explain to me what's happening?

    Thank you,
    Cruz

  2. #2
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Thanks
    101
    Thanked 15 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Why is this thread not running?

    Aaah, I get it. The compiler optimizes my thread away since it can't see that frameIdx will ever change its value. Declaring it
    Qt Code:
    1. volatile int frameIdx = 0;
    To copy to clipboard, switch view to plain text mode 
    Does the trick.

Similar Threads

  1. Replies: 0
    Last Post: 7th December 2017, 18:33
  2. Replies: 1
    Last Post: 6th December 2015, 16:12
  3. Replies: 4
    Last Post: 17th October 2013, 11:12
  4. how to terminate a thread when it is running
    By guchangyuan in forum Qt Programming
    Replies: 1
    Last Post: 19th October 2009, 10:50
  5. running() - Thread
    By sabeesh in forum Qt Programming
    Replies: 5
    Last Post: 9th October 2007, 18:45

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.