This preview shows page 1. Sign up to view the full content.
Unformatted text preview: ends on an intermediate result from the previous iteration. When we call rand repeatedly from a single thread after seeding it with a call to srand, we can expect a repeatable sequence of numbers. However, this assumption no longer holds if multiple threads are calling rand. The only way to make a function such as rand thread-safe is to rewrite it so that it does not use any static data, relying instead on the caller to pass the state information in arguments. The disadvantage is that the programmer is now forced to change the code in the calling routine as well. In a large program where there are potentially hundreds of different call sites, making such modiﬁcations could be non-trivial and error-prone. 11.6. THREAD-SAFE AND REENTRANT FUNCTIONS 593 3. Returning a pointer to a static variable. Some functions, such as gethostbyname, compute a result in a static structure and then return a pointer to that structure. If we call such functions from concurrent threads, then disaster is likely as results being used by one thread are suddenly overwritten by another thread. There...
View Full Document
- Spring '10
- The American