1 / 61

Principles of Parallel Programming First Edition by Calvin Lin Lawrence Snyder

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.

mglen
Télécharger la présentation

Principles of Parallel Programming First Edition by Calvin Lin Lawrence Snyder

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 6:Programming with Threads Principles of Parallel Programming First Edition by Calvin Lin Lawrence Snyder

  2. Code Spec 6.1pthread_create(). The POSIX Threads thread creation function.

  3. Code Spec 6.2pthread_join(). The POSIX Threads rendezvous function.

  4. Code Spec 6.3 pthread_self(). The POSIX Threads function to fetch a thread’s ID.

  5. Code Spec 6.4pthread_equal(). The POSIX Threads function to compare two thread IDs for equality.

  6. Code Spec 6.5pthread_exit(). The POSIX Threads thread termination function.

  7. Code Spec 6.6pthread attributes. An example of how thread attributes are set in the POSIX Threads interface.

  8. Code Spec 6.7 The POSIX Threads routines for acquiring and releasing mutexes.

  9. Code Spec 6.8 The POSIX Threads routines for dynamically creating and destroying mutexes.

  10. Code Spec 6.9 An example of how dynamically allocated mutexes are used in the POSIX Threads interface.

  11. Figure 6.1

  12. Code Spec 6.10pthread_cond_wait(). The POSIX Thread routines for waiting on condition variables.

  13. Code Spec 6.11pthread_cond_signal(). The POSIX Threads routines for signaling a condition variable.

  14. Figure 6.2

  15. Figure 6.3 Bounded buffer example using condition variables nonempty and nonfull.

  16. Figure 6.4 Example of why a signaling thread needs to be protected by a mutex.

  17. Figure 6.5

  18. Code Spec 6.12 The POSIX Threads routines for dynamically creating and destroying condition variables.

  19. 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.

  20. 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.

  21. Code Spec 6.14pthread_key_create(). POSIX Thread routine for creating a key for thread-specific data.

  22. Code Spec 6.15pthread_key_delete(). POSIX Thread routine for deleting a key.

  23. Code Spec 6.16pthread_setspecific(). POSIX Thread routine for setting the value of thread-specific data.

  24. Code Spec 6.17 pthread_getspecific(). POSIX Thread routine for getting the value of some thread-specific data.

  25. 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.

  26. 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.

  27. Figure 6.9 Monitor implementation in C++.

  28. Figure 6.9 Monitor implementation in C++. (cont.)

  29. 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.

  30. Figure 6.11 A program to check the invariants in the bounded buffer program, Figure 6.9.

  31. Figure 6.12 Multiple readers, single writer support routines.

  32. Figure 6.13 Multiple readers, single-writer support routines based on a single-conditionvariable, but subject to spurious wake-ups.

  33. Code Spec 6.18 POSIX Thread routine for setting thread scheduling attributes.

  34. Figure 6.14 A 2D relaxation replaces—on each iteration—all interior values by the average of their four nearest neighbors.

  35. Figure 6.15

  36. Figure 6.16 2D Sucessive over-relaxation program written using POSIX Threads.

  37. Figure 6.16 2D Sucessive over-relaxation program written using POSIX Threads. (cont.)

  38. Figure 6.16 2D Sucessive over-relaxation program written using POSIX Threads. (cont.)

  39. Figure 6.16 2D Sucessive over-relaxation program written using POSIX Threads. (cont.)

  40. Figure 6.17 It’s often profitable to do useful work while waiting for some long-latency operation to complete.

  41. 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.

  42. Figure 6.19 A 1D over-relaxation replaces—on each iteration—all interior values by the average of their two nearest neighbors.

  43. Figure 6.20 Program for 1D successive over-relaxation using a single-phase barrier.

  44. Figure 6.21 Program for 1D successive over-relaxation using a split-phase barrier.

  45. Figure 6.21 Program for 1D successive over-relaxation using a split-phase barrier. (cont.)

  46. Figure 6.22 Initial split-phase barrier implementation that keeps a count of the number of arrivals.

  47. Figure 6.22 Initial split-phase barrier implementation that keeps a count of the number of arrivals. (cont.)

  48. Figure 6.23 Deadlock with our initial implementation of a split-phase barrier; Thread0 and Thread1 each waits for different instances of the barrier.

  49. Figure 6.24 A correct barrier implementation that keeps track of the correct phase.

  50. Figure 6.24 A correct barrier implementation that keeps track of the correct phase. (cont.)

More Related