161115-SafeConcurrencyGuidelines.pdf

Consistent locking is sufficient to prevent data

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

Consistent locking is sufficient to prevent data races, but is not sufficient to prevent bad interleavings. For example, the extended example with stacks used consistent locking. Consistent locking is not necessary to prevent data races nor bad interleavings. It is a guideline that is typically the default assumption for concurrent code, violated only when carefully documented and for good reason. One good reason is when the program has multiple conceptual “phases” and all threads globally coordinate when the program moves from one phase to another. In this case, different phases can use different syn- chronization strategies. For example, suppose in an early phase multiple threads are inserting different key- value pairs into a dictionary. Perhaps there is one lock for the dictionary and all threads acquire this lock before performing any dictionary operations. Suppose that at some CPEN 221 – Fall 2016
Image of page 5

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

Guidelines for Safe Concurrent Programming 6 point in the program, the dictionary becomes fixed, meaning no more insertions or deletions will be performed on it. Once all threads know this point has been reached (probably by reading some other synchronized shared-memory object to make sure that all threads are done adding to the dictionary), it would be correct to perform subsequent lookup operations without synchronization since the dictionary has become immutable . 2.3 Guideline #2: Start with coarse-grained locking and move to finer- grained locking only if contention is hurting performance This guideline introduces some new terms. Coarse-grained locking means using fewer locks to guard more objects. For example, one lock for an entire dictionary or for an entire array of bank accounts would be coarse-grained. Conversely, fine-grained locking means using more locks, each of which guards fewer memory locations. For example, using a separate lock for each bucket in a chaining hashtable or a separate lock for each bank account would be fine-grained. The terms “coarse-grained” and “fine-grained” do not have a strict dividing line: we can really only say that one locking strategy is “more coarse-grained” or “more fine-grained” than another one. That is, locking granularity is really a continuum, where one direction is coarser and the other direction is finer. Coarse-grained locking is typically easier. After grabbing just one lock, we can access many different locations in a critical section. With fine-grained locking, operations may end up needing to acquire multiple locks (using nested synchronized statements). It is easy to forget to acquire a lock or to acquire the wrong lock. It also introduces the possibility of deadlock . But coarse-grained locking leads to threads waiting for other threads to release locks unnecessarily, i.e., in situations when no errors would result if the threads proceeded concurrently. In the extreme, the coarsest strategy would have just one lock for the entire program! Under this approach, it is obvious what lock to acquire, but no two operations on shared memory can proceed in parallel. Having one lock for many, many
Image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.
  • Fall '17
  • satish
  • Concurrent computing, Safe Concurrent Programming

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