This was one of the most important events in the

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: pec.tv_nsec = now.tv_usec * 1000; return &timespec; } code/threads/maketimeout u.c Figure 11.26: maketimeout u: A version of maketimeout that is not thread-safe. Figure 11.27 shows how we might use the lock-and-copy approach to create a thread-safe (but not reentrant) wrapper that the calling program can invoke instead of the original thread-unsafe function. Finally, we can go all out and rewrite the unsafe function so that it is reentrant, as shown in Figure 11.28. Notice that the calling thread now has the responsibility of passing an address that points to unshared data. 11.6. THREAD-SAFE AND REENTRANT FUNCTIONS 595 code/threads/maketimeout t.c 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include "csapp.h" struct timespec *maketimeout_u(int secs); static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; struct timespec *maketimeout_t(int secs) { struct timespec *sp; /* shared */ struct timespec *up = Malloc(sizeof(struct timespec)); /* unshared */ Pthread_mutex_lock(&mutex); sp = maketimeout_u(secs); *up...
View Full Document

This note was uploaded on 09/02/2010 for the course ELECTRICAL 360 taught by Professor Schultz during the Spring '10 term at BYU.

Ask a homework question - tutors are online