This preview shows page 1. Sign up to view the full content.
Unformatted text preview: urns: 0 if OK, non-zero on error The pthread detach function detaches the joinable thread tid. Threads can detach themselves by calling pthread detach with an argument of pthread self(). Even though some of our examples will use joinable threads, there are good reasons to use detached threads in real programs. For example, a high-performance Web server might create a new peer thread each time it receives a connection request from a Web browser. Since each connection is handled independently by a separate thread, it is unnecessary and indeed undesirable for the server to explicitly wait for each peer thread to terminate. In this case, each peer thread should detach itself before it begins processing the request so that its memory resources can be reclaimed after it terminates. Practice Problem 11.1:
A. The program in Figure 11.6 has a bug. The thread is supposed to sleep for one second and then print a string. However, when we run it, nothing prints. Why? B. You can ﬁx this bug by replacing the exit function in line 9 with one of two different Pthreads function calls....
View Full Document