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