[Lazarus] timer in a console application
Hans-Peter Diettrich
DrDiettrich1 at aol.com
Fri Aug 10 17:01:33 CEST 2012
Andrea Mauri schrieb:
> My app must be as fast as possible, is checksynchronize cpu demanding?
> Here a simplified representation of my app:
>
> begin
> loadMoleculeFiles;
> for i:= 0 to numberofmolecules-1 do
> begin
> ok:= calculatemoleculardescriptorsonmolecule(molecule_i);
> if not ok then
> writeln('time exceeded');
> end;
> end;
>
> calculatemoleculardescriptorsonmolecule can takes a lot of time (from
> few milliseconds to hours, if the molecule is huge or complex), I would
> like to put a user defined time limit to the calculation on a single
> molecule, if the time limit is reached the app should pass to the next
> molecule.
> so the timer should check, during calculations, if the time per molecule
> is reached, then stop the actual molecule calculation.
Why do you ever want to *stop* an thread?
Do you want to *kill* threads taking too much time, discarding all the
work done so far? Or do you want to resume them later?
If you want to get *all* calculations done, and minimize the *total*
time, you have to start the long running threads first. When e.g. one
thread takes more time than all others together, this thread will
determine the total runtime of the program. Obviously every delay of
this thread will increase the total runtime, leaving idle cores when all
other threads have terminated. This certainly is not desireable, instead
as many cores as available should have something to do all the time.
I'd sort the molecules by complexity, and start with the most complex
ones. Whenever a thread finishes, the next (less complex) thread is
started. If you want to see some results soon, one thread can start at
the opposite end of the list, with the least complex molecules, so that
you can continue with inspecing or working on these results, while the
other calculations proceed.
In the simplest case you create an thread for every molecule, in
*suspended* state, and put them all into a list (TThreadList?).
Determine the maximum number of threads, which should run concurrently,
e.g. from the number of cores of your CPU. Then resume that number of
threads, starting with the longest ones. Whenever a thread terminates,
it resumes the next waiting thread, until no more pending threads remain.
You also can implement an thread pool, with the known number of
concurrent threads. When such a thread starts, it gets the next molecule
from the pending list and starts calculation. When finished, the same
thread gets the next molecule from the list, and starts calculating this
one.
DoDi
More information about the Lazarus
mailing list