In terms of programming the basic model comprises

Info icon This preview shows pages 2–4. Sign up to view the full content.

In terms of programming, the basic model comprises multiple threads that are running in a mostly uncoordinated way. We might create a new thread when we have something new to do. The operations of each thread are interleaved (running alongside, before, after, or at the same time) with operations by other threads. We may have four threads processing bank-account changes as they arrive. While it is unlikely that two threads would access the same account at the same time, it is possible and we must be correct in this case. One thing that makes concurrent programs very difficult to debug is that the bugs can be very unlikely to occur. If a program exhibits a bug and you re-run the program another million times with the same inputs, the bug may not appear again. This is because what happens can depend on the order that threads access shared resources, which is not en- tirely under programmer control. It can depend on how the threads are scheduled onto the processors, i.e., when each thread is chosen to run and for how long, something that is decided automatically (when debugging, it often feels capriciously ) by the implemen- tation of the programming language, with help from the operating system. Therefore, concurrent programming is nondeterministic , the output does not depend only on the input. Because testing concurrent programs is so difficult, it is exceedingly important to design them well using well-understood design principles from the beginning. As an example, suppose a bank-account class has methods deposit and withdraw . Suppose the latter throws an exception if the amount to be withdrawn is larger than the current balance. If one thread deposits into the account and another thread withdraws from the account, then whether the withdrawing thread throws an exception could de- pend on the order the operations occur. And that is still assuming all of one operation completes before the other starts. In upcoming sections, we will learn how to use locks to ensure the operations themselves are not interleaved, but even after ensuring this, the program can still be nondeterministic. It is worth asking why anyone would use this difficult programming model. It would certainly be simpler to have one thread that does everything we need to do. There are several reasons: CPEN 221 – Fall 2016
Image of page 2

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

Basic Shared-Memory Concurrency 3 Parallelism : Despite conceptual differences between parallelism and concur- rency, it may well be that a parallel algorithm needs to have different threads accessing some of the same data structures in an unpredictable way. For exam- ple, we could have multiple threads search through a graph, only occasionally crossing paths. Responsiveness : Many programs, including operating systems and programs with user interfaces, want/need to respond to external events quickly. One way to do this is to have some threads doing the program’s expensive computations while other threads are responsible for “listening to” events like buttons being clicked or typing occurring. The listening threads can then (quickly) write to some fields that the computation threads later read.
Image of page 3
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.
  • Fall '17
  • satish
  • Subroutine, Concurrent computing, Basic Shared-Memory Concurrency

{[ 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