Board index » cppbuilder » multithreading and offtime precalculations

multithreading and offtime precalculations


2004-09-03 09:00:57 PM
cppbuilder62
Hi,
I have an application which, at times, does a fairly large number of
precalculations when the program is launched. On slow machines, it can take
a fairly long time (up to a few minutes in some cases), which means
displaying a splash screen for a long time, and confusing the user (and
making the application look dumb).
The sad part is that most of these calculations are related to very specific
parts of the program, which are not often used by average users.
So, I was thinking of using a separate thread to have things calculated
while the app is idle. The program would then launch very quickly, let the
user work with the parts of the application which do not use precalculation,
eventually waiting for them to finish when he needs them.
To do so, I need to have a simple way to check whether some part of the
precalculation is finished (so that the main app "knows" if some operation
can be done), and, if not, to signal that it is finished, so that the main
program can wait for it, given that the precalculation procedure consists in
several steps, and that I don't want to wait for the slowest one to finish.
Summarizing, it means :
1- launching a separate calculation thread which does the precalculation
while the app is idle (it might even not imply a separate thread, but just a
clever use the OnIdle handler),
2- let it signal that some part of the calculation is finished
3- have a simple way to test these signals in my main application, and wait
for them when needed (either by disabling "temporarily unavailable"
commands, or having such commands, wait for the signal...)
Actually, maybe it does not even imply several threads, but a clever use of
on_idle messages...
Now, once the problem is posed, I am a bit lost on where to start from...
Can anyone give me some directions ?
TIA
Francois
 
 

Re:multithreading and offtime precalculations

Try kicking off a thread using _beginthread. Avoid using the CreateThread
API (it leaks memory if you're calling any C RTL functions such as sprintf -
see Microsoft's website at www.msdn.com and type in CreateThread or
_beginthread in the search box in the upper right hand corner).
Detecting the end of the thread is easy (WaitforSingleObject), but I believe
you're looking for a flag to determine when a certain part of the code in
the thread has finished executing (and not necessarily an indication of
when the thread is finished)? In that case, I would declare a local
variable in your main program and pass it to the thread as a parameter
(generally not a good idea, unless you only have one thread running that
manipulates it).
There's a very good book, entitled "Multithreading Applications in Win32:
The Complete Guide to Threads" (ISBN: 0201442345). It goes through
threading and signalling fairly quickly (can probably read the first half in
a couple of hourse since it's easy to read) with details and examples.
"François Charton" < XXXX@XXXXX.COM >wrote in message
Quote
Hi,

I have an application which, at times, does a fairly large number of
precalculations when the program is launched. On slow machines, it can
take
a fairly long time (up to a few minutes in some cases), which means
displaying a splash screen for a long time, and confusing the user (and
making the application look dumb).

The sad part is that most of these calculations are related to very
specific
parts of the program, which are not often used by average users.

So, I was thinking of using a separate thread to have things calculated
while the app is idle. The program would then launch very quickly, let the
user work with the parts of the application which do not use
precalculation,
eventually waiting for them to finish when he needs them.

To do so, I need to have a simple way to check whether some part of the
precalculation is finished (so that the main app "knows" if some operation
can be done), and, if not, to signal that it is finished, so that the main
program can wait for it, given that the precalculation procedure consists
in
several steps, and that I don't want to wait for the slowest one to
finish.

Summarizing, it means :
1- launching a separate calculation thread which does the precalculation
while the app is idle (it might even not imply a separate thread, but just
a
clever use the OnIdle handler),
2- let it signal that some part of the calculation is finished
3- have a simple way to test these signals in my main application, and
wait
for them when needed (either by disabling "temporarily unavailable"
commands, or having such commands, wait for the signal...)

Actually, maybe it does not even imply several threads, but a clever use
of
on_idle messages...

Now, once the problem is posed, I am a bit lost on where to start from...
Can anyone give me some directions ?

TIA
Francois