Mypeekhelperwrong and isempty cpen 221 fall 2016 bad

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

View Full Document Right Arrow Icon
myPeekHelperWrong and isEmpty : CPEN 221 – Fall 2016
Image of page 5

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 6 If a stack is not empty, then isEmpty should return false . Suppose two threads share a stack stk that has one element in it. If one thread calls isEmpty and the other calls myPeekHelperWrong , the first thread can get the wrong result: Thread 1 Thread 2 (calls myPeekHelperWrong) -------- ---------------------------------- E ans = stk.pop(); boolean b = isEmpty(); stk.push(ans); return ans; The figure of stack states shows how this interleaving produces the wrong result when the stack has one element. Notice there is nothing wrong with what Java is doing here. It is the programmer who wrote myPeekHelperWrong such that it does not behave like a peek operation should. Yet the code that gets the wrong answer is isEmpty , which is another reason debugging concurrent programs is difficult. Figure 2: A bad interleaving for a stack with a peek operation that is incorrect in a concurrent program. myPeekHelperWrong and push : Another key property of stacks is that they return elements in last-in-first-out order. But consider this interleaving, where one of Thread 1’s push operations happens in the middle of a concurrent call to myPeekHelperWrong . CPEN 221 – Fall 2016
Image of page 6
Bad Interleavings and Data Races 7 Thread 1 Thread 2 (calls myPeekHelperWrong) -------- ---------------------------------- stk.push(x); E ans = stk.pop(); stk.push(y); stk.push(ans); return ans; E z = stk.pop(); This interleaving has the effect of reordering the top two elements of the stack, so z ends up holding the wrong value. myPeekHelperWrong and pop : Similar to the previous example, a pop operation (instead of a push ) that views the intermediate state of myPeekHelperWrong can get the wrong (not last-in-first-out) answer: Thread 1 Thread 2 (calls myPeekHelperWrong) -------- ---------------------------------- stk.push(x); stk.push(y); E ans = stk.pop(); E z = stk.pop(); stk.push(ans); return ans; myPeekHelperWrong and myPeekHelperWrong : Finally, two threads both calling myPeekHelperWrong causes race conditions. First, if stk initially has one element, then a bad interleaving of the two calls could raise a StackEmptyException , which should happen only when a stack has zero elements: Thread 1 Thread 2 (calls myPeekHelperWrong) -------- ---------------------------------- E ans = stk.pop(); E ans = stk.pop(); // exception! stk.push(ans); return ans; CPEN 221 – Fall 2016
Image of page 7

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 8 Second, even if the stack has more than 1 element, we can extend the interleaving above to swap the order of the top two elements, which will cause later pop operations to get the wrong answer. Thread 1 Thread 2 (calls myPeekHelperWrong) -------- ---------------------------------- E ans = stk.pop(); E ans = stk.pop(); stk.push(ans); return ans; stk.push(ans); return ans; In general, bad interleavings can involve any number of threads using any operations. The defense is to define large enough critical sections such that every possible thread schedule is correct. Because push , pop , and isEmpty are synchronized, we do not have to worry about calls to these methods being interleaved with each other. To implement peek correctly, the key insight is to realize that its intermediate state must not be vis- ible and therefore we need its calls to pop and push to be part of
Image of page 8
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