161115-SafeConcurrencyGuidelines.pdf

Having one lock for many many bank accounts is

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

View Full Document Right Arrow Icon
operations on shared memory can proceed in parallel. Having one lock for many, many bank accounts is probably a bad idea because with enough threads, there will be multi- ple threads attempting to access bank accounts at the same time and they will be unable to. Contention describes the situation where threads are blocked waiting for each other: They are contending (in the sense of competing) for the same resource, and this is hurt- ing performance. If there is little contention (it is rare that a thread is blocked), then coarse-grained locking is sufficient. When contention becomes problematic, it is worth considering finer-grained locking, realizing that changing the code without introduc- ing race conditions is difficult. A good execution profiler 1 should provide information 1 A profiler is a tool that reports where a run of a program is spending time or other resources. CPEN 221 – Fall 2016
Image of page 6

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 7 on where threads are most often blocked waiting for a lock to be released. 2.4 Guideline #3: Make critical sections large enough for correctness but no larger. Do not perform I/O or expensive computations within crit- ical sections. The granularity of critical sections is completely orthogonal to the granularity of locks (guideline #2). Coarse-grained critical sections are “large” pieces of code whereas fine- grained critical sections are “small” pieces of code. However, what matters is not how many lines of code a critical section occupies. What matters is how long a critical sec- tion takes to execute (longer means larger) and how many shared resources such as memory locations it accesses (more means larger). If you make your critical sections too short, you are introducing more possible inter- leavings and potentially incorrect race conditions. For example, compare: synchronized (lk) { /* do first thing */ } /* do second thing */ synchronized (lk) { /* do third thing */ } with: synchronized (lk) { /* do first thing */ /* do second thing */ /* do third thing */ } The first version has smaller critical sections. Smaller critical sections lead to less con- tention. For example, other threads can use data guarded by lk while the code above is doing the “second thing.” But smaller critical sections expose more states to other threads. Perhaps only the second version with one larger critical section is correct. This guideline is therefore a tough one to follow because it requires moderation: make crit- ical sections no longer or shorter than necessary. Basic operations like assignment statements and method calls are so fast that it is al- most never worth the trouble of splitting them into multiple critical sections. However, expensive computations inside critical sections should be avoided wherever possible for the obvious reason that other threads may end up blocked waiting for the lock. Re- member in particular that reading data from disk or the network is typically orders of CPEN 221 – Fall 2016
Image of page 7
Guidelines for Safe Concurrent Programming 8 magnitude slower than reading memory. Long running loops and other computations should also be avoided.
Image of page 8

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

View Full Document Right Arrow Icon
Image of page 9
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