[Proj] libproj4 thread safety

Patrick Mézard pmezard at gmail.com
Wed Feb 23 04:25:18 EST 2005

[This message is mostly about libproj4. If the PROJ.4 mailing list is
not the right place to post, please redirect me to a more suitable

1- I am currently reading libproj4 manual and I reached the section
concerning error management, which basically sums up to checking the
global variable pj_errno like C-Runtime "errno".
In "lib_proj.h", pj_errno is declared like :

extern int		/* global error return code */

and "pj_errno.c" just defines it as an int.

I would like to use libproj4 in a multithreaded (win32) environment.
What is the status of libproj4 regarding thread-safety ? I gave a
quick look to the rest of the code and everything looks allright apart
from the pj_errno global. On this topic, the mailing list archives are
pretty mute, there is one reference in a JNI discussion :

"-- GDAL and Proj both use the C runtime library extensively and the C
runtime is not threadsafe, but java is thread safe and therefore if you
have multiple java threads trying to use your JNI functions you will get
strange/random behaviors (including crashing) of your app.  The way to
solve this is easy, you simply mark your java functions as syncronized
using the syncronized key word.  It will be slower under multiple user
conditions, but at least it will never crash from threading issues."

Is it relevant to libproj4 ? As far as I know, a few broken functions
aside (strtok), C-Runtime is mostly thread-safe on platforms I know,
so I do not really see the point here.

Could I assume that pj_errno is the only libproj4 part which is not
threadsafe ? (not that I'd be glad to live with that, but it could be
corrected). Is anybody interested in having regular -thread safe-
return codes instead (or in plus of the global variable) ? Do you
think it can be done with a small amount of work, while leaving the
current interface unchanged (apart from adding return values to "void"
functions or adding new functions with new prototypes) ?

2- Are the opaque structures returned by pj_init thread-safe, that is,
are they "const" in a C++ sense ? Or should I create one per thread ?

Patrick Mézard

More information about the Proj mailing list