610 likes | 628 Views
Chapter 6: Programming with Threads. Principles of Parallel Programming First Edition by Calvin Lin Lawrence Snyder. Code Spec 6.1 pthread_create() . The POSIX Threads thread creation function. Code Spec 6.2 pthread_join() . The POSIX Threads rendezvous function.
E N D
Chapter 6:Programming with Threads Principles of Parallel Programming First Edition by Calvin Lin Lawrence Snyder
Code Spec 6.1pthread_create(). The POSIX Threads thread creation function.
Code Spec 6.2pthread_join(). The POSIX Threads rendezvous function.
Code Spec 6.3 pthread_self(). The POSIX Threads function to fetch a thread’s ID.
Code Spec 6.4pthread_equal(). The POSIX Threads function to compare two thread IDs for equality.
Code Spec 6.5pthread_exit(). The POSIX Threads thread termination function.
Code Spec 6.6pthread attributes. An example of how thread attributes are set in the POSIX Threads interface.
Code Spec 6.7 The POSIX Threads routines for acquiring and releasing mutexes.
Code Spec 6.8 The POSIX Threads routines for dynamically creating and destroying mutexes.
Code Spec 6.9 An example of how dynamically allocated mutexes are used in the POSIX Threads interface.
Code Spec 6.10pthread_cond_wait(). The POSIX Thread routines for waiting on condition variables.
Code Spec 6.11pthread_cond_signal(). The POSIX Threads routines for signaling a condition variable.
Figure 6.3 Bounded buffer example using condition variables nonempty and nonfull.
Figure 6.4 Example of why a signaling thread needs to be protected by a mutex.
Code Spec 6.12 The POSIX Threads routines for dynamically creating and destroying condition variables.
Figure 6.6 Example of thread-specific data in POSIX Threads. Thread-specific data are accessed by keys, which map to different memory locations in different threads.
Code Spec 6.13 Example of how thread-specific data is used. Once initialized with this code, any procedure can access the value of my_index.
Code Spec 6.14pthread_key_create(). POSIX Thread routine for creating a key for thread-specific data.
Code Spec 6.15pthread_key_delete(). POSIX Thread routine for deleting a key.
Code Spec 6.16pthread_setspecific(). POSIX Thread routine for setting the value of thread-specific data.
Code Spec 6.17 pthread_getspecific(). POSIX Thread routine for getting the value of some thread-specific data.
Figure 6.7 Deadlock example. Threads T1 and T2 hold locks L1 and L2, respectively, and each thread attempts to acquire the other lock, which cannot be granted.
Figure 6.8 Monitors provide an abstraction of synchronization in which only one thread can access the monitor’s data at any time. Other threads are blocked either waiting to enter the monitor or waiting on events inside the monitor.
Figure 6.10 Monitors and invariants. The shaded circles represent program states in which the invariants may be violated. The empty circles represent program states in which the invariants are assumed to be true.
Figure 6.11 A program to check the invariants in the bounded buffer program, Figure 6.9.
Figure 6.12 Multiple readers, single writer support routines.
Figure 6.13 Multiple readers, single-writer support routines based on a single-conditionvariable, but subject to spurious wake-ups.
Code Spec 6.18 POSIX Thread routine for setting thread scheduling attributes.
Figure 6.14 A 2D relaxation replaces—on each iteration—all interior values by the average of their four nearest neighbors.
Figure 6.16 2D Sucessive over-relaxation program written using POSIX Threads.
Figure 6.16 2D Sucessive over-relaxation program written using POSIX Threads. (cont.)
Figure 6.16 2D Sucessive over-relaxation program written using POSIX Threads. (cont.)
Figure 6.16 2D Sucessive over-relaxation program written using POSIX Threads. (cont.)
Figure 6.17 It’s often profitable to do useful work while waiting for some long-latency operation to complete.
Figure 6.18 A split-phase barrier allows a thread to do useful work while waiting for the other threads to arrive at the barrier.
Figure 6.19 A 1D over-relaxation replaces—on each iteration—all interior values by the average of their two nearest neighbors.
Figure 6.20 Program for 1D successive over-relaxation using a single-phase barrier.
Figure 6.21 Program for 1D successive over-relaxation using a split-phase barrier.
Figure 6.21 Program for 1D successive over-relaxation using a split-phase barrier. (cont.)
Figure 6.22 Initial split-phase barrier implementation that keeps a count of the number of arrivals.
Figure 6.22 Initial split-phase barrier implementation that keeps a count of the number of arrivals. (cont.)
Figure 6.23 Deadlock with our initial implementation of a split-phase barrier; Thread0 and Thread1 each waits for different instances of the barrier.
Figure 6.24 A correct barrier implementation that keeps track of the correct phase.
Figure 6.24 A correct barrier implementation that keeps track of the correct phase. (cont.)