PDA

View Full Version : Terminating a thread.



kiranraj
6th July 2007, 17:36
Hi ,

I have a thread created in main thread which is created in the heap.

Up on some user input i need to terminate this thread.

I have written a fuction TerminateThread

TerminateThread()
{
m_mutex.lock()
m_flag = true; // m_mutex and m_flag are global. The other thread will check this flag after some operations, if the flag is set ,it will call terminate( return).
m_mutex.unlock();
delete m_thread ;
//Delete statement is giving an error saying trying to delete a running thread.

So i replaced the code with this
// Since after the thread terminates m_thread->isRunning() = false
while ( m_thread->isRunning()) {
wait(4000); //This is making the thread to wait indefinetly.
}

}

I have also tried
m_thread->terminate()
m_thread->wait(); // Never returns from the wait call
But even this is not working.

Thanks.

jpn
7th July 2007, 09:45
You will have to make the thread to return from QThread::run(). Please search the forums, this subject has been discussed quite a few times.

Bitto
8th July 2007, 15:02
You could be running into a kernel bug:

https://bugzilla.novell.com/show_bug.cgi?id=289641

Have you tried compiling Qt with no exceptions? (-no-exceptions).

gri
9th July 2007, 12:14
If you simply start "exec()" in your "thread::run()" method you can stop the thread by calling "quit()" and the "wait()" method would work