PDA

View Full Version : Threads (follow-up to "Screen refresh during heavy computation")



martinb0820
18th November 2008, 21:00
My initial problem was to allow the main window to refresh during heavy computation that couldn't be broken into smaller chunks. Based on information from wysota, that item is resolved - the computation is in its own thread, and the main thread continues to process events.

The problem now is that, if the main thread is killed, I haven't been able to cleanly kill the computation thread. I've tried numerous combinations of catching the QCloseEvent and calling terminate() on the thread, QApplication::closeAllWindows(), ::exit(), etc.

I'm using a Win32 console window during development (via AllocConsole()) and one thing I noticed is that the console window's "x" stops all the threads. Is there a straightforward platform independent way to obtain that effect in Qt?

Thanks in advance!
Martin

wysota
18th November 2008, 22:18
Make your computation thread check some global variable from time to time and if it is true, return from run(). Then you can raise that flag at any time from your main thread.

If injecting code into the thread is not an option for you (for instance when the thread simply executes one long function that you can't modify), your only option is to wait for it to finish or kill the process (as you do by closing the console window).

martinb0820
18th November 2008, 23:59
Thanks for the reply.

Killing the process would be preferable to waiting for the computation to end. (It is very tricky stuff, in Fortran, that I'd rather not disturb.)

Is there a platform-independent way to kill the process using Qt calls, or do I need to use native O/S calls? I'm supporting both Windows and Linux...

Martin

wysota
19th November 2008, 10:52
Is there a platform-independent way to kill the process using Qt calls, or do I need to use native O/S calls? I'm supporting both Windows and Linux.

Divide something by 0 :)

For posix systems you can use kill passing it your own pid and SIGKILL as the signal. For non-posix systems you have to find other means.

martinb0820
11th December 2008, 18:35
The following is effective in Windows. I just didn't want to have to resort to an #ifdef!

void VisGui::closeEvent(QCloseEvent *event)
{
cout << "Received closeEvent" << endl;

#ifdef WIN32
// http://support.microsoft.com/default.aspx?scid=KB;en-us;q178893
TerminateProcess(
OpenProcess(SYNCHRONIZE|PROCESS_TERMINATE, FALSE, _getpid()),
0);
#endif

///\todo Equivalent for Linux
}