Well, after a quick test, it seems that terminate is not working under windows (at least, not working as expected by reading the manual).
That's a very good occasion for you to re-think your code and make it work without terminate. There is nothing less clean than terminate (just remind that, amongst other risks, terminate() can let your memory allocator in an undefined state, crashing your whole app). Terminate should'nt even be in Qt framework IMHO... You can't say than adding some code to poll for a flag and exit the worker code is not clean. It's just some extra work, for the stake of having an app that will not segfault. Etienne 2013/6/21 francois cellier <f_cell...@yahoo.fr> > Thanks Etienne and Mandeep for your answers. > > However as I wrote in my initial post (in the code of the objectInThread > class), in my real application I do not have a while loop just a sequence > of functions. With a while loop and the flag the code is clean like that. > However, in my case that means I need to check that flag from many classes. > For now I did the approach that you were talking using a singleton to get > an easy access to that flag from many different classes and I find this not > so clean... > > However even without speaking about clean things or things to avoid, I > would like to know why the terminate function does not terminate my thread > if an eventloop is running. > > > ------------------------------ > *De :* Etienne Sandré-Chardonnal <etienne.san...@m4x.org> > *À :* > *Cc :* "interest@qt-project.org" <interest@qt-project.org> > *Envoyé le :* Vendredi 21 juin 2013 13h00 > *Objet :* 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 > > >
_______________________________________________ Interest mailing list Interest@qt-project.org http://lists.qt-project.org/mailman/listinfo/interest