[Gdal-dev] Re: Error handler threading suggestion

Frank Warmerdam fwarmerdam at gmail.com
Mon Jul 25 13:49:25 EDT 2005


On 7/21/05, Steve Soule <Steve.Soule at vexcel.com> wrote:
> Currently, the GDAL error handler is thread-local.  If the client wants
> the error handler to be something other than CPLDefaultErrorHandler,
> CPLSetErrorHandler must be called by each thread created.  This is
> painful.
> 
> Currently there is an error-handler stack, maintained as
> psHandlerStack, with pfnErrorHandler implicitly the top of the
> stack.  CPLPushErrorHandler and CPLPopErrorHandler maintain the stack,
> and CPLSetErrorHandler replaces the top handler on the stack.  The stack
> must be thread-local, because push and pop are used to temporarily turn
> off error reporting in many places in GDAL.  It would be nice if
> CPLSetErrorHandler set the handler for all threads.
> 
> Here's how to do it:  let pfnErrorHandler implicitly be the bottom
> of the stack instead of the top of the stack.  Push and Pop would
> not change pfnErrorHandler.  The two places the error handler is
> called, CPLErrorV and CPLDebug, would be changed to check the stack
> first, and call the top handler on the stack if the stack were not
> empty, and only call pfnErrorHandler if the stack were empty.
> Then pfnErrorHandler would not need to be thread-local, and
> CPLSetErrorHandler would set the base error handler for all threads.
> CPLPushErrorHandler and CPLPopErrorHandler would override the base
> error handler for a single thread.

Steve,

I agree.  I have changed the model as you suggest, and now
the "error handler stack" is thread-local and takes precidence over
error handlers installed with CPLSetErrorHandler().  The handler
installed with CPLSetErrorHandler() is global to all threads. 

In single threaded applications this does potentially mean there is 
a change in how error handlers work in some applications.  In particular,
you can no longer be confident that CPLSetErrorHandler() will change
the error handler that will be used for the next error.  If there have
been error handlers pushed with CPLPushErrorHandler() then these
will take precidence.   However, I think this is a rare circumstance. 

I have updated the documentation accordingly.

Best regards,
-- 
---------------------------------------+--------------------------------------
I set the clouds in motion - turn up   | Frank Warmerdam, warmerdam at pobox.com
light and sound - activate the windows | http://pobox.com/~warmerdam
and watch the world go round - Rush    | Geospatial Programmer for Rent




More information about the Gdal-dev mailing list