<html><body><div style="color:#000; background-color:#fff; font-family:tahoma, new york, times, serif;font-size:12pt">Thanks Etienne and Mandeep for your answers.<br><br>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...<br><br>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.<br><br><div><br></div>  <div style="font-family: tahoma, new york, times, serif; font-size: 12pt;"> <div style="font-family: times new roman, new york, times,
 serif; font-size: 12pt;"> <div dir="ltr"> <hr size="1">  <font face="Arial" size="2"> <b><span style="font-weight:bold;">De :</span></b> Etienne Sandré-Chardonnal <etienne.sandre@m4x.org><br> <b><span style="font-weight: bold;">À :</span></b>  <br><b><span style="font-weight: bold;">Cc :</span></b> "interest@qt-project.org" <interest@qt-project.org> <br> <b><span style="font-weight: bold;">Envoyé le :</span></b> Vendredi 21 juin 2013 13h00<br> <b><span style="font-weight: bold;">Objet :</span></b> Re: [Interest] Terminating a QThread<br> </font> </div> <div class="y_msg_container"><br><div id="yiv434973816"><div dir="ltr"><div>Sorry Mandeep, I didn't read your post first. Basically what I posted is similar.<br><br></div>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</div><div class="yiv434973816gmail_extra"><br><br><div class="yiv434973816gmail_quote">2013/6/21 Mandeep Sandhu <span dir="ltr"><<a rel="nofollow" ymailto="mailto:mandeepsandhu.chd@gmail.com" target="_blank" href="mailto:mandeepsandhu.chd@gmail.com">mandeepsandhu.chd@gmail.com</a>></span><br>
<blockquote class="yiv434973816gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"><div dir="ltr"><div class="yiv434973816gmail_extra"><div class="yiv434973816gmail_quote"><div class="yiv434973816im">On Fri, Jun 21, 2013 at 1:36 PM, francois cellier <span dir="ltr"><<a rel="nofollow" ymailto="mailto:f_cellier@yahoo.fr" target="_blank" href="mailto:f_cellier@yahoo.fr">f_cellier@yahoo.fr</a>></span> wrote:<br>

<blockquote class="yiv434973816gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex;"><div><div style="font-size:12pt;font-family:tahoma, new york, times, serif;">Dear all,<br><br>Even if I know that it can be dangerous to terminate a thread, I need to do it for my application.<br>

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.<br><br>

I have tried the worker approach with the following code and I did not find a way to terminate the thread :<br><br>For my thread class :<br><div><ol><li><div>ThreadEx<span>::</span><span>ThreadEx</span><span>(</span><a rel="nofollow" target="_blank" href="http://qt-project.org/doc/QObject.html"><span>QObject</span></a> <span>*</span>parent<span>)</span> <span>:</span></div>

</li><li><div>    <a rel="nofollow" target="_blank" href="http://qt-project.org/doc/QThread.html"><span>QThread</span></a><span>(</span>parent<span>)</span></div></li><li><div><span>{</span></div></li><li><div><span>}</span></div></li>
<li>
<div> </div></li><li><div><span>void</span> ThreadEx<span>::</span><span>run</span><span>(</span><span>)</span></div></li><li><div><span>{</span></div></li><li><div>    <a rel="nofollow" target="_blank" href="http://qt-project.org/doc/QThread.html"><span>QThread</span></a><span>::</span><span>setTerminationEnabled</span><span>(</span><span>true</span><span>)</span><span>;</span></div>

</li><li><div>    exec<span>(</span><span>)</span><span>;</span></div></li><li><div><span>}</span></div></li></ol></div><br><br>For my worker class:<br><ol><li><div>ObjectInThread<span>::</span><span>ObjectInThread</span><span>(</span><a rel="nofollow" target="_blank" href="http://qt-project.org/doc/QObject.html"><span>QObject</span></a> <span>*</span>parent<span>)</span> <span>:</span></div>

</li><li><div>    <a rel="nofollow" target="_blank" href="http://qt-project.org/doc/QObject.html"><span>QObject</span></a><span>(</span>parent<span>)</span></div></li><li><div><span>{</span></div></li><li><div><span>}</span></div></li>
<li>
<div> </div></li><li><div> </div></li><li><div><span>void</span> ObjectInThread<span>::</span><span>run</span><span>(</span><span>)</span></div></li><li><div><span>{</span></div></li><li><div>   <span>int</span> compteur <span>=</span> <span>0</span><span>;</span></div>

</li><li><div><span>//here i am using a loop instead of the long linear flow  but it is not a loop  in the real code</span></div></li><li><div>    <span>while</span><span>(</span><span>1</span><span>)</span></div></li><li>

<div>    <span>{</span></div></li><li><div>        <span>qDebug</span><span>(</span><span>)</span> <span><<</span>compteur<span>;</span></div></li><li><div>        compteur<span>++;</span></div></li><li><div>        Sleep<span>(</span><span>1000</span><span>)</span><span>;</span></div>

</li><li><div>    <span>}</span></div></li><li><div><span>}</span></div></li></ol></div></div></blockquote></div><div>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).<br>

</div><div>In your main(GUI) thread you can wait for your worker thread to exit:<br><br><span></span>m_thread<span></span>->quit();<br><span></span>m_thread<span></span>->wait();<br></div><div><br></div><div>HTH,<br>

</div><div>-mandeep<br><br></div><div><br></div><div>This should cleanly exit the thread.<br> <br></div><blockquote class="yiv434973816gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex;">
<div class="yiv434973816im">
<div><div style="font-size:12pt;font-family:tahoma, new york, times, serif;">Moreover I tried the other way of using QThread that consists in inheriting the QThread class and overriding the run
 method with the code :<span><br><br>void</span> ThreadEx<span>::</span><span>run</span><span>(</span><span>)</span><span><br>{</span><br><span>    </span>int compteur = 0;<br><span>    </span>while(1)<br><span>    </span>{<br>

         qDebug() <<compteur;<br><span>    </span><span>    </span> compteur++;<br>         sleep(1);<br><span>    </span>}<br>}<br><br>In that case terminate works but I do not have an eventloop as I did not call the exec method.<br>

<br>Is this the expected behaviour ?<br><br>I am using Qt5.1 on centOS / RedHat 6.2. I have also tried this on Windows with no more success.<br><br><div>Thanks for your
 help,</div><div>François</div><div><br></div><div><br></div></div></div><br></div>_______________________________________________<br>
Interest mailing list<br>
<a rel="nofollow" ymailto="mailto:Interest@qt-project.org" target="_blank" href="mailto:Interest@qt-project.org">Interest@qt-project.org</a><br>
<a rel="nofollow" target="_blank" href="http://lists.qt-project.org/mailman/listinfo/interest">http://lists.qt-project.org/mailman/listinfo/interest</a><br>
<br></blockquote></div><br></div></div>
<br>_______________________________________________<br>
Interest mailing list<br>
<a rel="nofollow" ymailto="mailto:Interest@qt-project.org" target="_blank" href="mailto:Interest@qt-project.org">Interest@qt-project.org</a><br>
<a rel="nofollow" target="_blank" href="http://lists.qt-project.org/mailman/listinfo/interest">http://lists.qt-project.org/mailman/listinfo/interest</a><br>
<br></blockquote></div><br></div>
</div><br>_______________________________________________<br>Interest mailing list<br><a ymailto="mailto:Interest@qt-project.org" href="mailto:Interest@qt-project.org">Interest@qt-project.org</a><br><a href="http://lists.qt-project.org/mailman/listinfo/interest" target="_blank">http://lists.qt-project.org/mailman/listinfo/interest</a><br><br><br></div> </div> </div>  </div></body></html>