[Proj] libproj4 thread safety

Paul Selormey paul at toolscenter.org
Sun Apr 24 09:55:01 EDT 2005

Is there any need to really make the libproj4 thread safe?
I think any program that is well-written should be able to
handle the thread safety without having to build any thread
issues into the library.

Are these solutions being provided here applicable to 
other platforms beside Linux?

Best regards,

> -----Original Message-----
> From: proj-bounces at xserve.flids.com 
> [mailto:proj-bounces at xserve.flids.com] On Behalf Of Gerald Evenden
> Sent: Sunday, April 24, 2005 9:00 AM
> To: PROJ.4 and general Projections Discussions
> Subject: Re: [Proj] libproj4 thread safety
> The problem of the thread safe version of pj_errno for
> libproj4 seems to be taken care of with the following file:
> #include <pthread.h>
>          static pthread_once_t
> key_once = PTHREAD_ONCE_INIT;
>          static pthread_key_t
> key;
>          static void
> destroy(void *buf) { free(buf); }
>          static void
> create(void) { pthread_key_create(&key, destroy); }
>          int *
> pj_errno_loc(void) {
>          void *p;
>          pthread_once(&key_once, create);
>          if (!(p = pthread_getspecific(key))) {
>                  p = (void *)malloc(sizeof(int));
>                  pthread_setspecific(key, p);
>          }
>          return (int *)p;
> }
> and the following addition to a general header file:
> extern int *pj_errno_loc();
> #define pj_errno (*pj_errno_loc())
> Note that some return error checks are not made but what to 
> do if they fail??  Can't set pj_errno. :-(  Maybe should just 
> call one of the exit procedures.
> Next problem is how to handle this procedure for both those 
> interested in threads and those not caring a whit.  If it is 
> placed in libproj4.a, users have to link with '-l threads'
> regardless of using threads.  Make two different libraries?
> Do not include pj_errno.o library and force user to include 
> pj_errno.o on compile line?  Note: I will include a compile 
> switch to allow for compiling as either a vanilla or pthreads 
> version---it makes no difference to the macro inclusion nor 
> the remainder of the libproj4 system.
> Comments, suggestions would be appreciated here.
> I will run some more rigorous tests of the above with a 
> scaffold program before installing in the next release.
> Lastly, thanks for the help on this problem.  Trying to learn 
> about threads from on-line manuals is not my preferred 
> method.  In this case I completely missed the use of 
> 'pthread_once' which is critical to this solution.
> _____________________________________
> Jerry and the low riders: Daisy Mae and Joshua.
> "The whole religious complexion of the modern world is due to 
> the absence from Jerusalem of a lunatic asylum." Havelock Ellis, 1914
> _______________________________________________
> Proj mailing list
> Proj at xserve.flids.com
> http://xserve.flids.com/mailman/listinfo/proj

More information about the Proj mailing list