OSF DCE Application Development Guide--Introduction and Style Guide

OSF DCE Application Development Guide—Introduction and Style Guide
TABLE 2-1. Thread-Safe Calls
__________________________________________________
_sleep( ) accept( ) atfork( ) calloc( )
catclose( ) catgets() catopen() cfree( )
close( ) connect( ) creat( ) ctermid( )
cuserid( ) dup( ) dup2( ) fclose()
fcntl() fdopen( ) fflush() fgetc( )
fgets( ) fopen() fork() fprintf( )
fputc( ) fputs( ) fread( ) free()
freopen( ) fscanf() fseek( ) ftell( )
fwrite( ) getc( ) getchar( ) gets( )
getw( ) isatty( ) malloc( ) mktemp( )
open() pclose( ) pipe() popen()
printf( ) putc() putchar( ) puts()
putw() read() readv( ) realloc( )
recv() recvfrom() recvmsg( ) rewind()
scanf( ) select() send() sendmsg( )
sendto( ) setbuf( ) setbuffer( ) setlinebuf( )
setvbuf( ) sigaction( ) sigwait( ) sleep()
socket( ) socketpair() sprintf( ) sscanf()
system( ) tempnam() tmpfile( ) tmpnam()
ttyname( ) ttyslot() vfprintf( ) vprintf()
vsprintf( ) write( ) writev()
__________________________________________________
What follows is a summary of the thread-safety rules that should be followed when using
the pthread facilities. The list is by no means comprehensive; it describes the places
where multithreaded applications most frequently go wrong.
Access to all shared objects should be protected by the appropriate synchronization
mechnisms. The pthread global lock is not appropriate for such synchronization.
Mutexes should be used only to protect resources held for a short period of time. In
particular, note that pthread_mutex_lock( ) is not a cancellation point. Resources
needing to be held exclusively for a long time should be protected by condition
variables rather than mutexes, as this will not inhibit cancelability (see Section
2.3.3.2).
A shared object should be protected by only one mutex.
Be sure to use the available thread-safe library calls. These may be available as
wrapped routines, via the pthread.h header file, or your implementation may supply
reentrant libraries which must be linked with DCE applications.
Avoid nonwrapped process-blocking system calls, such as wait( ).
When threads need to acquire more than one mutex at a time, create a locking
sequence and require that all threads follow the sequence.
Do not make any assumptions about the atomicity of operations, as these are unlikely
to be portable.
In general, to avoid priority inversion, when three or more threads of different
priorities access a lock, associate a priority with the lock and force any thread to raise
its priority to the lock priority before acquiring the lock. Note that the default
2 6 Tandem Computers Incorporated 124246