PDA

View Full Version : QThread exit()/quit() question



TheKedge
28th August 2006, 14:15
Hello all,
gotta question:
i've started my thread, everything's great. Now I'd like to end it. Calling exit() exits the threads event loop. I assume it does so immediately. But in order to make sure that my thread really has finished, I'll need to do something like the following:


int BaseDevice::stopThread ()
{
quit();
if(wait ( interval() ))
;//ok
else{
log("stopThread() timed out", _TIMEOUT_ERROR);
//terminate(); //dangerous
}
return 0;
}
Thus I have a function that will wait until run() is finished. Question: does it work that way? That is, does wait() wait for event loop to exit (in which case the above doesn't work) or does it wait for run() to exit?

thanks
K

high_flyer
28th August 2006, 14:38
That is, does wait() wait for event loop to exit (in which case the above doesn't work) or does it wait for run() to exit?
Well in the docs it says:

bool QThread::wait ( unsigned long time = ULONG_MAX )

Blocks the thread until either of these conditions is met:

* The thread associated with this QThread object has finished execution (i.e. when it returns from run()). This function will return true if the thread has finished. It also returns true if the thread has not been started yet.
* time milliseconds has elapsed. If time is ULONG_MAX (the default), then the wait will never timeout (the thread must return from run()). This function will return false if the wait timed out.

This provides similar functionality to the POSIX pthread_join() function.