[fpc-devel] Exceptions in threads

Michael Van Canneyt michael at freepascal.org
Thu Dec 11 14:33:08 CET 2008



On Thu, 11 Dec 2008, Mattias Gärtner wrote:

> Zitat von Florian Klaempfl <florian at freepascal.org>:
> 
> > Mattias Gärtner schrieb:
> > > I'm writing a unit to simplify parallel methods/procedures.
> > >
> > > For example:
> > >   DoParallel(@AMethod,StartIndex,EndIndex,Data);
> > >
> > > The AMethod is executed with several threads in parallel.
> > >
> > > What should happen when an exception occurs?
> > > It would be nice if the exception can be transfered to the main thread.
> > > Is this possible? How?
> >
> > See synchronize/checksynchronize implementation.
> 
> Sorry, I misused the term 'main thread'.
> 
> DoParallel starts AMethod with several threads in parallel and waits until all
> threads finished. DoParallel can be called by any thread (e.g. the main thread
> of the process *or* any other sub thread). Even the threads of the pool can
> call again DoParallel. This is only limited by the memory, as each call creates
> a threadgroup object.
> 
> The goal is to simplify parallelizing procedures so for the user DoParallel
> should work like a normal procedure call.
> That's why it would be nice if the following could work:
> 
> try
>   ...
>   DoParallel(...);
>   ...
> except
> end;
> 
> The exception can occur in any of the subthreads including the starting thread
> (which I named 'main thread', because it is the main thread of the group. Maybe
> I should call it 'group thread' or 'starter thread').
> 
> When an exception occurs, the group waits for all other threads of this group to
> finish and then the exception should happen 'as if the exception occured in the
> starting thread'.

The way I uderstand threads this can never work, because then there should
be a synchronization wait loop after the doparallel.  Without the wait loop
it'll never work, because where (at which statement) should the exception be
raised ?

To make it operable, the thread should catch the exception, and then do a
synchronize with a procedure that raises the exception, so that when
synchronize is called, the exception is raised.

Michael.


More information about the fpc-devel mailing list