Also notice there are no data races because array and

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

View Full Document Right Arrow Icon
Also notice there are no data races because array and size are accessed only while holding the this lock. Not holding the lock in the constructor is fine because a new object is not yet reachable from any other thread. The new object that the constructor produces (i.e., the new instance of Stack ) will have to be assigned to some thread- shared location before a second thread could use it, and such an assignment will not happen until after the constructor finishes executing 1 . Now suppose we want to implement a new operation for our stack called peek that returns the newest not-yet-popped element in the stack without popping it. (This op- eration is also sometimes called top .) A correct implementation would be: synchronized E peek () { if (index== 0 ) throw new StackEmptyException (); return array[index -1 ]; } If we omit the synchronized keyword, then this operation would cause data races with simultaneous push or pop operations. Consider instead this alternate also-correct implementation: synchronized E peek () { E ans = pop (); push (ans); return ans; } This version is perhaps worse style, but it is certainly correct. It also has the advantage that this approach could be taken by a helper method outside the class where the first approach could not since array and index are private fields: 1 This would not necessarily be the case if the constructor did something like someObject.f = this; , but doing such things in constructors is usually a bad idea. CPEN 221 – Fall 2016
Image of page 4

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

View Full Document Right Arrow Icon
Bad Interleavings and Data Races 5 class C { static <E> E myPeekHelper (Stack<E> s) { synchronized (s) { E ans = s. pop (); s. push (ans); return ans; } } } Notice this version could not be written if stacks used some private inaccessible lock. Also notice that it relies on reentrant locks. However, we will consider instead this wrong version where the helper method omits its own synchronized statement: class C { static <E> E myPeekHelperWrong (Stack<E> s) { E ans = s. pop (); s. push (ans); return ans; } } Notice this version has no data races because the pop and push calls still acquire and release the appropriate lock. Also notice that myPeekHelperWrong uses the stack oper- ators exactly as it is supposed to. Nonetheless, it is incorrect because a peek operation is not supposed to modify the stack. While the overall result is the same stack if the code runs without interleaving from other threads, the code produces an intermediate state that other threads should not see or modify. This intermediate state would not be observable in a single-threaded program or in our correct version that made the entire method body a critical section. To show why the wrong version can lead to incorrect behavior, we can demonstrate interleavings with other operations such that the stack does the wrong thing. Writing out such interleavings is good practice for reasoning about concurrent code and helps determine what needs to be in a critical section. As it turns out in our small example, myPeekHelperWrong causes race conditions with all other stack operations, including itself.
Image of page 5
Image of page 6
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