161115-SafeConcurrencyGuidelines.pdf

An operation is atomic as in indivisible if it

Info icon This preview shows pages 9–11. Sign up to view the full content.

View Full Document Right Arrow Icon
An operation is atomic , as in indivisible, if it executes either entirely or not at all, with no other thread able to observe that it has partly executed. (In the databases literature, this would be called atomic and isolated , but it is common in concurrent programming to conflate these two ideas under the term atomic .) Ensuring necessary operations ap- pear to be atomic is exactly why we have critical sections. Since this is the essential point, this guideline recommends thinking first in terms of what the critical sections CPEN 221 – Fall 2016
Image of page 9

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

View Full Document Right Arrow Icon
Guidelines for Safe Concurrent Programming 10 are without thinking about locking granularity . Then, once the critical sections are clear, how to use locks to implement the critical sections can be the next step. In this way, we ensure we actually develop the software we want — the correct critical sections — without being distracted prematurely by the implementation details of how to do the locking. In other words, think about atomicity (using guideline #3) first and the locking protocol (using guidelines #0, #1, and #2) second. Unfortunately, one of the most difficult aspects of lock-based programming is when software needs change. When new critical sections are needed in “version 2.0” of a program, it may require changing the locking protocol, which in turn will require care- fully modifying the code for many other already-working critical sections. When we will discuss deadlocks, we will even see an example where there is really no good solu- tion. Nonetheless, following guideline #4 makes it easier to think about why and how a locking protocol may need changing. 2.6 Guideline #5: Do not implement your own concurrent data struc- tures. Use carefully tuned ones written by experts and provided in standard libraries. Widely used libraries for popular programming languages already contain many reusable data structures that you should use whenever they meet your needs. For example, there is little reason in Java to implement your own hashtable since the Hashtable class in the standard library has been carefully tuned and tested. For concurrent programming, the advice to “reuse existing libraries” is even more im- portant, precisely because writing, debugging, and testing concurrent code is so difficult. Experts who devote their lives to concurrent programming can write tricky fine-grained locking code once and the rest of us can benefit. For example, the ConcurrentHashMap class implements a hashtable that can be safely used by multiple threads with very little contention. It uses some techniques more advanced than discussed in these notes, but clients to the library have such trickiness hidden from them. For basic things like queues and dictionaries, do not implement your own.
Image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

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