Hi,

I would like to create a thread that is polling a serial port continuously. The application will be a deamon or a service (according the platform). But for now I'm developing using a Console application on Windows because it is more easy to debug.

The idea should be to create a thread and this thread will run forever or until the console application is closed. But just before the console closed itself it should wait until the thread finished. Thus, the console application should signal to the thread to stop. The main loop of the thread could poll a boolean or an event for that. This is what I call closing an application properly.

Now, I read a lot of documentation of QT Reference, Google, etc... But I never get this working *properly*.

Once I click on the X button of the console, or pressing key CTRL+C, the application close itself without having chance to signal to the thread to stop. How can I free resource used by the thread before it stop ? Worst, I'm asking to myself how the thread is stop ? Does it stop because it parent process has been killed ? If yes, this can lead to memory leak.

Here is what I've done (test code, forget about scope, bad design please or deriving class from QThread):

Qt Code:
  1. #include <QCoreApplication>
  2. #include <QThread>
  3. #include <QDebug>
  4. #include <csignal>
  5.  
  6. class WorkerThread : public QThread
  7. {
  8. public:
  9. explicit WorkerThread (QObject *parent = 0)
  10. :m_bStopThread(false) { }
  11.  
  12. private:
  13. void run()
  14. {
  15. // Thread run forever or until m_bStopThread = true
  16. while (!m_bStopThread)
  17. {
  18. qDebug() << "Worker thread operation... " << currentThreadId();
  19. QThread::msleep(1000);
  20. }
  21. return; // This breakpoint will never be triggered once the application is stop using X button of the windows or CTRL+C keyboard keys.
  22. }
  23. public :
  24. volatile bool m_bStopThread;
  25.  
  26. public Q_SLOTS:
  27.  
  28. void aboutToQuitApp()
  29. {
  30. m_bStopThread = true; // This breakpoint will never be triggered once the application is stop using X button of the windows or CTRL+C keyboard keys.
  31. }
  32. };
  33.  
  34. // This struct is a tips that I get from a forum that is supposed to catch the application closing. But it does not really work at all.
  35. struct CleanExit
  36. {
  37. static WorkerThread* pWorkerThread;
  38.  
  39. CleanExit(WorkerThread* _pWorkerThread)
  40. {
  41. CleanExit::pWorkerThread = _pWorkerThread;
  42.  
  43. signal(SIGINT, &CleanExit::exitQt);
  44. signal(SIGTERM, &CleanExit::exitQt);
  45. signal(SIGBREAK, &CleanExit::exitQt) ;
  46. }
  47.  
  48. static void exitQt(int sig)
  49. {
  50. // This breakpoint will be triggered only while using CTRL+C to close the application.
  51. // But the debugger loses his way as soon as I step further...
  52. // It is as if the application was already being closed and that I have no control on the
  53. // worker thread. The system has kill the thread because its parent process has been killed.
  54. pWorkerThread->m_bStopThread = true;
  55. pWorkerThread->wait();
  56.  
  57. QCoreApplication::exit(0);
  58. }
  59. };
  60. WorkerThread* CleanExit::pWorkerThread = NULL;
  61.  
  62. int main(int argc, char *argv[])
  63. {
  64. QCoreApplication a(argc, argv);
  65.  
  66. WorkerThread wk;
  67. CleanExit cleanExit(&wk);
  68.  
  69. QObject::connect(&wk, SIGNAL(finished()), &a, SLOT(quit()));
  70.  
  71. // The signal aboutToQuit [B]does not seem to work using Console Application[/B].
  72. QObject::connect(&a, SIGNAL(aboutToQuit()), &wk, SLOT(aboutToQuitApp()));
  73.  
  74. qDebug() <<"Starting worker thread ..." << QThread::currentThreadId();
  75. wk.start();
  76.  
  77. // Wait until the worker thread has finished his work !
  78. wk.wait();
  79.  
  80. // Close the console application
  81. return a.quit();
  82. }
To copy to clipboard, switch view to plain text mode 

Just read the comments, all my questions/problem are here.

Best regards,