Advanced Programming in the UNIX® Environment

(lily) #1
ptg10805159

442 Thread Control Chapter 12


The value of theprocess-sharedattribute can be eitherPTHREAD_PROCESS_SHARED
(accessible to threads from multiple processes) or PTHREAD_PROCESS_PRIVATE
(accessible to only threads in the process that initialized the barrier).

12.5 Reentrancy


We discussed reentrant functions and signal handlers in Section 10.6. Threads are
similar to signal handlers when it comes to reentrancy.Inboth cases, multiple threads
of control can potentially call the same function at the same time.

basename getchar_unlocked getservent putc_unlocked
catgets getdate getutxent putchar_unlocked
crypt getenv getutxid putenv
dbm_clearerr getgrent getutxline pututxline
dbm_close getgrgid gmtime rand
dbm_delete getgrnam hcreate readdir
dbm_error gethostent hdestroy setenv
dbm_fetch getlogin hsearch setgrent
dbm_firstkey getnetbyaddr inet_ntoa setkey
dbm_nextkey getnetbyname l64a setpwent
dbm_open getnetent lgamma setutxent
dbm_store getopt lgammaf strerror
dirname getprotobyname lgammal strsignal
dlerror getprotobynumber localeconv strtok
drand48 getprotoent localtime system
encrypt getpwent lrand48 ttyname
endgrent getpwnam mrand48 unsetenv
endpwent getpwuid nftw wcstombs
endutxent getservbyname nl_langinfo wctomb
getc_unlocked getservbyport ptsname

Figure 12.9 Functionsnotguaranteed to be thread-safe by POSIX.1

If a function can be safely called by multiple threads at the same time, we say that
the function isthread-safe.All functions defined in the Single UNIX Specification are
guaranteed to be thread-safe, except those listed in Figure12.9. In addition, the
ctermidandtmpnamfunctions arenot guaranteed to be thread-safe if they arepassed
anull pointer.Similarly,there is no guarantee thatwcrtombandwcsrtombsare
thread-safe when they arepassed a null pointer for theirmbstate_targument.
Implementations that support thread-safe functions will define the
_POSIX_THREAD_SAFE_FUNCTIONSsymbol in<unistd.h>.Applications can also
use the_SC_THREAD_SAFE_FUNCTIONSargument withsysconfto check for support
of thread-safe functions at runtime. Prior to Version 4 of the Single UNIX Specification,
all XSI-conforming implementations wererequired to support thread-safe functions.
With SUSv4, however,thread-safe function support is now required for an
implementation to be considered POSIX conforming.
With thread-safe functions, implementations provide alternative, thread-safe
versions of some of the POSIX.1 functions that aren’t thread-safe. Figure12.10 lists the
Free download pdf