Hello!

By now I've read the QtAssistant documentation regarding QThreads and even used a little of them in some projects, but to be honest I'm still not sure I'm working with them in the correct, safe method. To be more specific, I'm still not sure I understood the way by wich the run() method should be used alongisde the functions exec(), start(), quit(), terminate() and wait() as well as the use of flags to control the thread's event loop.

To give some context, now I'm working on a project where I need to emit a signal each 250 ms and I decided it would be better to use a thred to do the work. So I need a event loop that, once the thread is started, will do the job, plus a previous configuration. The run() function, therefore, looked like this:

Qt Code:
  1. void DataBrain::run()
  2. {
  3. qDebug() << "run()";
  4.  
  5. switch (source.principal)
  6. {
  7. case SourceInternet:
  8. switch (source.internet)
  9. {
  10. case SourceYahoo:
  11. connect(this,SIGNAL(signalUpdateData()),this,SLOT(slotUpdateYahoo()));
  12. break;
  13.  
  14. case SourceGoogle:
  15. connect(this,SIGNAL(signalUpdateData()),this,SLOT(slotUpdateGoogle()));
  16. D_TODO("DataBrain::run() | SourceGoogle");
  17. break;
  18.  
  19. default:
  20. M_BUG_OCCURANCE("DataBrain::run() | SourceInternet");
  21. break;
  22. }
  23. break;
  24.  
  25. case SourceRealTime:
  26. D_TODO("DataBrain::run() | SourceRealTime");
  27. break;
  28.  
  29. case SourceLocal:
  30. M_BUG_OCCURANCE("DataBrain::run()() | currentSource == SourceLocal");
  31. break;
  32. }
  33.  
  34. while(true)
  35. {
  36. emit signalUpdateData();
  37.  
  38. msleep(250);
  39. }
  40.  
  41. qDebug() << "run end";
  42. }
To copy to clipboard, switch view to plain text mode 

In doing so, I was expecting, given what I read in the QtAssistant data about QThread, that once I call the start() function from this thread, the run() function would be executed, doing the configuration and then running the event loop eternaly till a quit() or terminate() function was called, with the detail that if I call terminate(), the last qDebug() would never be called, while by using quit() it would since QtAssistant says that quit() ends the thread's event loop and finish it correctly (by which I understood: runs run() till the end). What I noticed, though, in a first moment was not just that the last qDebug() was never called, but also that if I called quit() and then start() again, the thread would immediately start again inside the while(true) code, never passing again by the configuration procedure and the first qDebug(). This was making quite the problem for my code till I was able to fix this, despite I still don't know exactly how ^^ (I guess it was because I put a "wait(500);" after my call of quit().

I was beginning to think that my problem was solved when I read this topic in QtCentre: [http://www.qtcentre.org/threads/5590...qDebug+thread], where anda_skoa wrote that the proper way of handling a thread is to create a stop flag (that is, something like "while(flag)" instead of "while(true)"). I've done that already in previous works, but I still would like to know exactly why shouldn't I use my way, with the calling of quit(), instead of this flag-based method.

More than that, it's still not clear to me when should I use terminate() instead of quit(), given the fact that that method is avaliable for us programmers to use despite being risky. Can I call it at least in a thread's destructor? And for what exactly the method exec() is used in thread programming, given the fact that there is the function start()? And also how exactly the method quit() works? And since we are already talking about threads, when exactly it's appropriate to use QSemaphore instead of QMutex/QMutexLocker and vice-versa?



Thanks and sorry for too many questions in one topic.

Momergil