<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="gmail_extra"><br><br><div class="gmail_quote">2013/6/21 Mandeep Sandhu <span dir="ltr"><<a href="mailto:mandeepsandhu.chd@gmail.com" target="_blank">mandeepsandhu.chd@gmail.com</a>></span><br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div class="im">On Fri, Jun 21, 2013 at 1:36 PM, francois cellier <span dir="ltr"><<a href="mailto:f_cellier@yahoo.fr" target="_blank">f_cellier@yahoo.fr</a>></span> wrote:<br>

<blockquote class="gmail_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 href="http://qt-project.org/doc/QObject.html" target="_blank"><span>QObject</span></a> <span>*</span>parent<span>)</span> <span>:</span></div>

</li><li><div>    <a href="http://qt-project.org/doc/QThread.html" target="_blank"><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 href="http://qt-project.org/doc/QThread.html" target="_blank"><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 href="http://qt-project.org/doc/QObject.html" target="_blank"><span>QObject</span></a> <span>*</span>parent<span>)</span> <span>:</span></div>

</li><li><div>    <a href="http://qt-project.org/doc/QObject.html" target="_blank"><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="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<div class="im">
<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 href="mailto:Interest@qt-project.org" target="_blank">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></blockquote></div><br></div></div>
<br>_______________________________________________<br>
Interest mailing list<br>
<a 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></blockquote></div><br></div>