A thread may be waiting for something to happen

Info icon This preview shows pages 5–8. Sign up to view the full content.

• A thread may be waiting for something to happen before it continues. For ex- ample, the next section discusses the join primitive where one thread does not continue until another thread has terminated. Let’s be more concrete about what a thread is and how threads communicate. It is helpful to start by enumerating the key pieces that a sequential program has while it is running . CPEN 221 – Fall 2016
Image of page 5

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

Introduction to Concurrent and Parallel Programming 6 Figure 1: Sequential State. The key pieces of an executing sequential program: A pro- gram counter, a call stack, and a heap of objects. (There are also static fields of classes, which we can think of as being in the heap.) CPEN 221 – Fall 2016
Image of page 6
Introduction to Concurrent and Parallel Programming 7 • One call stack , where each stack frame holds the local variables for a method call that has started but not yet finished. Calling a method pushes a new frame and returning from a method pops a frame. Call stacks are why recursion is not “magic.” • One program counter . This is just a low-level name for keeping track of what statement is currently executing. In a sequential program, there is exactly one such statement. • Static fields of classes. • Objects. An object is created by calling new , which returns a reference to the new object. We call the memory that holds all the objects the heap . (This use of the word “heap” has nothing to do with heap data structure used to implement priority queues.) It is separate memory from the memory used for the call stack and static fields. With this overview of the sequential program state , it is much easier to understand threads: Each thread has its own call stack and program counter, but all the threads share one collection of static fields and objects. • When a new thread starts running, it will have its own new call stack. It will have one frame on it, which is like that thread’s main , but it won’t actually be main . • When a thread returns from its first method, it terminates. • Each thread has its own program counter and local variables, so there is no “inter- ference” from other threads for these things. The way loops, calls, assignments to variables, exceptions, etc. work for each thread is just like you learned in se- quential programming and is separate for each thread. • What is different is how static fields and objects work. In sequential program- ming we know x.f=42; y = x.f; always 42 to the variable y . But now the object that x refers to might also have its f field written to by other threads, so we cannot be so sure. In practice, even though all objects could be shared among threads, most are not. In fact, just as having static fields is often poor style, having lots of objects shared among threads is often poor style. But we need some shared objects because that is how threads communicate. If we are going to create parallel algorithms where helper threads run in parallel to compute partial answers, they need some way to communicate those partial answers back to the “main” thread. The way we will do it is to have the helper threads
Image of page 7

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

Image of page 8
This is the end of the preview. Sign up to access the rest of the document.
  • Fall '17
  • satish
  • Central processing unit, shared memory, Parallel algorithm

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern