161115-SynchronizationPrimitives.pdf

Here is an incomplete list of other primitives you

Info icon This preview shows pages 12–13. Sign up to view the full content.

View Full Document Right Arrow Icon
Here is an incomplete list of other primitives you may encounter: Semaphores are rather low-level mechanisms where two operations, called P and V for historical reasons, increment or decrement a counter. There are rules about how these operations are synchronized and when they block. Semaphores can be easily used to implement locks as well as barriers. Barriers are a bit more like join and are often more useful in parallel program- ming than concurrent programming. When threads get to a barrier, they block until n of them (where n is a property of the barrier) reach the barrier. Unlike join , the synchronization is not waiting for one other thread to terminate , but rather n other threads to get to the barrier . Monitors provide synchronization that corresponds more closely to the struc- ture of your code. Like an object, they can encapsulate some private state and provide some entry points (methods). As synchronization, the basic idea is that only one thread can call any of the methods at a time, with the others blocking. This is very much like an object in Java where all the methods are synchronized on the this lock, which is exactly Java’s standard idiom. Therefore, it is not un- common for Java’s locks to be called monitors. We did not use this term because the actual primitive Java provides is a reentrant lock even though the common idiom is to code up monitors using this primitive. CPEN 221 – Fall 2016
Image of page 12

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

View Full Document Right Arrow Icon
Reader/Writer Locks, Condition Variables and Other Synchronization Primitives 13 4 Summary Simple locks help us resolve some synchronization issues when writing concurrent pro- grams. There are, however, situations when we need other primitives. In this reading, we discussed two in some detail: reader/writer locks that permit multiple threads from reading shared data and if the shared memory region is being written to then only the writer thread has access. condition variables that permit multiple threads to wait for an event to occur. These two primitives are often used. Apart from these, we also briefly looked at three other primitives: semaphores, barriers and monitors. By picking the synchronization primitives carefully, based on context, we can keep our programs safe from bugs and easy to understand . The right primitives also make our software ready for change . CPEN 221 – Fall 2016
Image of page 13
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