161115-SafeConcurrencyGuidelines.pdf

Dictionaries storing already computed results can

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

random for each thread to use its own object. Dictionaries storing already-computed results can also be thread-local: This has the disadvantage that one thread will not insert a value that another thread can then use, but the decrease in synchronization is often worth this trade-off. Note that because each thread’s call-stack is thread-local, there is never a need to syn- chronize on local variables. So using local variables instead of object fields can help where possible, but this is good style anyway even in single-threaded programming. Overall, the vast majority of objects in a typical concurrent program should and will be thread-local. It may not seem that way in these notes, but only because we are focusing on the shared objects, which are the difficult ones. In conclusion, do not share objects unless those objects really have the explicit purpose of enabling shared-memory communication among threads. CPEN 221 – Fall 2016
Image of page 2

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

Guidelines for Safe Concurrent Programming 3 1.2 More Immutable Memory Whenever possible, do not update fields of objects: Make new objects instead. This guideline is a key tenet of functional programming , which unfortunately is typically not taught in the same course as concurrency. Even in single-threaded programs, it is of- ten helpful to avoid mutating fields of objects because other too-often-forgotten-about aliases to that object will “see” any mutations performed. Let’s consider a small example that demonstrates some of the issues. Suppose we have a dictionary such as a hashtable that maps student ids to names where the name class is represented like this: class Name { public String first; public String middle; public String last; public Name(String f, String m, String l) { first = f; middle = m; last = l; } public String toString () { return first + ” ” + middle + ” ” + last; } } Suppose we want to write a method that looks up a student by id in some table and returns the name, but using a middle initial instead of the full middle name. The fol- lowing direct solution is probably the best style : Name n = table. lookup (id); return n. first + ” ” + n. middle . charAt ( 0 ) + ” ” + n. last ; Notice that the computation is almost the same as that already done by toString . So if the computation were more complicated than just a few string concatenations, it would be tempting and arguably reasonable to try to reuse the toString method. This approach is a bad idea : Name n = table. lookup (id); n. middle = n. middle . substring ( 0 , 1 ); return n. toString (); CPEN 221 – Fall 2016
Image of page 3
Guidelines for Safe Concurrent Programming 4 While this code produces the right answer, it has the side-effect of changing the name’s middle field. Worse, the Name object is still in the table . Actually, this depends on how the lookup method is implemented. We assume here it returns a reference to the object in the table, rather than creating a new Name object. If Name objects are not mutated, then returning an alias is simpler and more efficient. Under this assumption, the full middle name for some student has been replaced in the table by the middle initial, which is presumably a bug.
Image of page 4

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

Image of page 5
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