Hints on POSIX Threads


A typical thread program has the structure
  #define _REENTRANT
You need to include the pthread library when compiling:
  gcc -g -lpthread -D_REENTRANT
or, for the Solaris compiler,
  cc -mt -lpthread

The program will compile without it, but will exit immediately, with the thread creation routines returning a value of -1.

Be sure to check each thread routine for error return values, in particular if the program isn't working properly. Generally speaking, the thread routines return zero on success and non-zero (-1 or a positive integer) on error. If the return value is a positive integer, see /usr/include/sys/errno.h for the mapping, with the call-specific reasons found on the man page. Use strerror() to convert the error value to a text string, but be careful to only pass it positive values.



Last updated by Henning Schulzrinne