12.4 Thread Safety

Team-FLY

A hidden problem with threads is that they may call library functions that are not thread-safe, possibly producing spurious results. A function is thread-safe if multiple threads can execute simultaneous active invocations of the function without interference. POSIX specifies that all the required functions, including the functions from the standard C library, be implemented in a thread-safe manner except for the specific functions listed in Table 12.2. Those functions whose traditional interfaces preclude making them thread-safe must have an alternative thread-safe version designated with an _r suffix.

An important example of a function that does not have to be thread-safe is strerror . Although strerror is not guaranteed to be thread-safe, many systems have implemented this function in a thread-safe manner. Unfortunately, because strerror is listed in Table 12.2, you can not assume that it works correctly if multiple threads call it. We use strerror only in the main thread, often to produce error messages for pthread_create and pthread_join . Section 13.7 gives a thread-safe implementation called strerror_r .

Another interaction problem occurs when threads access the same data. The individual copier threads in Program 12.8 work on independent problems and do not interact with each other. In more complicated applications, a thread may not exit after completing its assigned task. Instead, a worker thread may request additional tasks or share information. Chapter 13 explains how to control this type of interaction by using synchronization primitives such as mutex locks and condition variables .

Table 12.2. POSIX functions that are not required to be thread-safe.

asctime

fcvt

getpwnam

nl_langinfo

basename

ftw

getpwuid

ptsname

catgets

gcvt

getservbyname

putc_unlocked

crypt

getc_unlocked

getservbyport

putchar_unlocked

ctime

getchar_unlocked

getservent

putenv

dbm_clearerr

getdate

getutxent

pututxline

dbm_close

getenv

getutxid

rand

dbm_delete

getgrent

getutxline

readdir

dbm_error

getgrgid

gmtime

setenv

dbm_fetch

getgrnam

hcreate

setgrent

dbm_firstkey

gethostbyaddr

hdestroy

setkey

dbm_nextkey

gethostbyname

hsearch

setpwent

dbm_open

gethostent

inet_ntoa

setutxent

dbm_store

getlogin

l64a

strerror

dirname

getnetbyaddr

lgamma

strtok

dlerror

getnetbyname

lgammaf

ttyname

drand48

getnetent

lgammal

unsetenv

ecvt

getopt

localeconv

wcstombs

encrypt

getprotobyname

localtime

wctomb

endgrent

getprotobynumber

lrand48

 

endpwent

getprotoent

mrand48

 

endutxent

getpwent

nftw

 

In traditional UNIX implementations , errno is a global external variable that is set when system functions produce an error. This implementation does not work for multithreading (see Section 2.7), and in most thread implementations errno is a macro that returns thread-specific information. In essence, each thread has a private copy of errno . The main thread does not have direct access to errno for a joined thread, so if needed, this information must be returned through the last parameter of pthread_join .

Team-FLY


Unix Systems Programming
UNIX Systems Programming: Communication, Concurrency and Threads
ISBN: 0130424110
EAN: 2147483647
Year: 2003
Pages: 274

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net