However if host a sends a frame to host c on a

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: ;); rc = pthread_cond_timedwait(&cond, &mutex, maketimeout(&timeout, 1)); if (rc != ETIMEDOUT) { printf("WHEW!\n"); exit(0); } } printf("BOOM!\n"); exit(0); } /* thread routine */ void *thread(void *vargp) { getchar(); Pthread_mutex_lock(&mutex); Pthread_cond_signal(&cond); Pthread_mutex_unlock(&mutex); return NULL; } code/threads/timebomb.c Figure 11.24: timebomb.c: A beeping timebomb that explodes unless the user hits a key within 5 seconds. 592 CHAPTER 11. CONCURRENT PROGRAMMING WITH THREADS 11.6 Thread-safe and Reentrant Functions When we program with threads, we must be careful to write functions that are thread-safe. A function is thread-safe if and only if it will always produce correct results when called repeatedly within multiple concurrent threads. If a function is not thread-safe, then it is said to be thread-unsafe. We can identify four (non-disjoint) classes of thread-unsafe functions: 1. Failing to protect shared variables. We have already encountered this problem with the count function in Figure 11.8 that increments an...
View Full Document

Ask a homework question - tutors are online