I have been assigned the task of making a library thread-safe. I'm
seeking advice on the following issues:
1) Sun's "Multithreaded Programming Guide" states that modules of a
multi-threaded application should be compiled with either -D_REENTRANT
or -D_POSIX_C_SOURCE=199506L (or both). To get a single-threaded
application, none of these flags should be defined. Page 167 of the
May 2002 version of this guide says: "This applies to every module of
Does this imply that it is impossible to create a single thread-safe
version of the library that can be linked into both single-threaded
and multi-threaded applications?
[I would prefer to not have to create two or perhaps four versions of
the library for each possible combination of the above mentioned
2) The library has an error code global variable, much like `errno' in
the Standard C library. This error code variable must somehow be
converted into thread-specific data.
The nicest solution would be to use thread-local storage (TLS), with
support from the linker, thread-library, compiler, and assembler. The
Solaris 9 12/02 "Linker and Libraries Guide" suggests that the linker
supports TLS, but I cannot find anything about how to use this from
within C source code.
Perhaps this feature is not quite ready for use yet?
Otherwise, I suppose I must use the pthread_key_create,
pthread_setspecific, and pthread_getspecific functions? And the key
must be created using a pthread_once_t control unless the user is
required to create the key explicitly?
Thanks in advance for any advice on these questions.
Hugin Expert A/S
Niels Jernes Vej 10
9220 Aalborg East