This preview shows page 1. Sign up to view the full content.
Unformatted text preview: are two ways to deal with this class of thread-unsafe function. One option is to rewrite the function so that the caller passes the address of the structure to store the results in. This eliminates all shared data, but it requires the programmer to change the code in the caller as well. If the thread-unsafe function is difﬁcult or impossible to modify (e.g., it is linked from a library), then another option is to use what we call the lock-and-copy approach. The idea is to associate a mutex with the thread-unsafe function. At each call site, lock the mutex, call the thread-unsafe function, dynamically allocate memory for the result, copy the result returned by the function to this memory, and then unlock the mutex. An attractive variant is to deﬁne a thread-safe wrapper function that performs the lock-and-copy, and then replace all calls to the thread-unsafe function with calls to the wrapper. 4. Calling thread-unsafe functions. If a function too. calls a thread-unsafe function, then is thread-unsafe, Thread-safety can be a con...
View Full Document
- Spring '10
- The American