But a bool value can be cached indefinitely, so make sure you declare the flag as volatile!
Tony From: interest-bounces+tony=rightsoft.com...@qt-project.org [mailto:interest-bounces+tony=rightsoft.com...@qt-project.org] On Behalf Of Etienne Sandré-Chardonnal Sent: Friday, 21 June 2013 9:01 PM Cc: interest@qt-project.org Subject: Re: [Interest] Terminating a QThread Sorry Mandeep, I didn't read your post first. Basically what I posted is similar. A mutex is a little bit overkill here, you could use an atomic variable if you want to be sure, but just a bool flag will be enough as it is always set atomically AFAIK 2013/6/21 Mandeep Sandhu <mandeepsandhu....@gmail.com> On Fri, Jun 21, 2013 at 1:36 PM, francois cellier <f_cell...@yahoo.fr> wrote: Dear all, Even if I know that it can be dangerous to terminate a thread, I need to do it for my application. The function that run into the thread is like a long linear sequence of code that has not been designed in a signal/slot way. Moreover, in my thread, I need an eventloop as I use in it some TCP or UDP Qt sockets. I have tried the worker approach with the following code and I did not find a way to terminate the thread : For my thread class : 1. ThreadEx::ThreadEx(QObject <http://qt-project.org/doc/QObject.html> *parent) : 2. QThread <http://qt-project.org/doc/QThread.html> (parent) 3. { 4. } 5. 6. void ThreadEx::run() 7. { 8. QThread <http://qt-project.org/doc/QThread.html> ::setTerminationEnabled(true); 9. exec(); 10. } For my worker class: 1. ObjectInThread::ObjectInThread(QObject <http://qt-project.org/doc/QObject.html> *parent) : 2. QObject <http://qt-project.org/doc/QObject.html> (parent) 3. { 4. } 5. 6. 7. void ObjectInThread::run() 8. { 9. int compteur = 0; 10. //here i am using a loop instead of the long linear flow but it is not a loop in the real code 11. while(1) 12. { 13. qDebug() <<compteur; 14. compteur++; 15. Sleep(1000); 16. } 17. } You use a condition variable in your loop here, which will exit out of the run() function. This variable can be set from your stop() slot (protected by a mutex if needed). In your main(GUI) thread you can wait for your worker thread to exit: m_thread->quit(); m_thread->wait(); HTH, -mandeep This should cleanly exit the thread. Moreover I tried the other way of using QThread that consists in inheriting the QThread class and overriding the run method with the code : void ThreadEx::run() { int compteur = 0; while(1) { qDebug() <<compteur; compteur++; sleep(1); } } In that case terminate works but I do not have an eventloop as I did not call the exec method. Is this the expected behaviour ? I am using Qt5.1 on centOS / RedHat 6.2. I have also tried this on Windows with no more success. Thanks for your help, François _______________________________________________ Interest mailing list Interest@qt-project.org http://lists.qt-project.org/mailman/listinfo/interest _______________________________________________ Interest mailing list Interest@qt-project.org http://lists.qt-project.org/mailman/listinfo/interest
_______________________________________________ Interest mailing list Interest@qt-project.org http://lists.qt-project.org/mailman/listinfo/interest