Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

Chapter 5. Using POSIX Threads > Reentrant Code and Compiler Flags

Reentrant Code and Compiler Flags

Depending upon the platform and compiler, it may be necessary to compile with special flags to indicate that the application is multithreaded. On some platforms, gcc has the flag -pthread, which links in the POSIX threading library and defines _REENTRANT. The Solaris Studio compiler has the flag -mt, which performs the same task; only the flag defined is __REENTRANT.

Defining the preprocessor flag includes a set of “reentrant” functions to go with the usual set. The reentrant versions are denoted by the _r postfix. The reason these functions exist is that many of the C library functions are stateful, meaning that a call to the function sets up some state that is then used in later function calls. This statefulness means that multiple threads cannot call the function at the same time without the state becoming corrupted. The reentrant versions of the functions typically use a structure that the calling thread provides to hold the state for that calling thread. The code in Listing 5.75 uses the readdir() call to print out the contents of the current directory.


  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free 10-Day Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint