If both threads were writing it would also be a data

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

(If both threads were writing, it would also be a data race.) Interestingly, simultaneous calls to getBalance do not cause a data race because simultaneous reads are not an error. But if one thread calls getBalance at the same time another thread calls set- Balance , then a data race occurs. Even though setBalance is synchronized, getBal- ance is not, so they might read and write balance at the same time. If your program has data races, it is infeasible to reason about what might happen. Figure 1: Visual depiction of a data race: Two threads accessing the same field of the same object, at least one of them writing to the field, without synchronization to ensure the accesses cannot happen, “at the same time.” CPEN 221 – Fall 2016
Image of page 2

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

Bad Interleavings and Data Races 3 2 Bad Interleavings: An Example with Stacks A useful way to reason about bad interleavings is to think about intermediate states that other threads must not “see.” Data structures and libraries typically have invariants necessary for their correct operation. For example, a size field may need to store the correct number of elements in a data structure. Operations typically violate invariants temporarily, restoring the invariant after making an appropriate change. For exam- ple, an operation might add an element to the structure and increment the size field. No matter what order is used for these two steps, there is an intermediate state where the invariant does not hold. In concurrent programming, our synchronization strat- egy often amounts to using mutual exclusion to ensure that no thread can observe an invariant-violating intermediate state produced by another thread. What invariants matter for program correctness depends on the program. Let us consider an extended example using a basic implementation of a bounded-size stack. (This is not an interesting data structure; the point is to pick a small example so we can focus on the interleavings. There are alternatives to throwing exceptions for this kind of data type.) class Stack<E> { private E[] array; private int index = 0 ; Stack( int size) { array = (E[]) new Object[size]; } synchronized boolean isEmpty () { return index== 0 ; } synchronized void push (E val) { if (index==array. length ) throw new StackFullException (); array[index++] = val; } synchronized E pop () { if (index== 0 ) throw new StackEmptyException (); return array[--index]; } } CPEN 221 – Fall 2016
Image of page 3
Bad Interleavings and Data Races 4 The key invariant is that if index > 0 then index-1 is the position of the most re- cently pushed item that has not yet been popped. Both push and pop temporarily vi- olate this invariant because they need to modify index and the contents of the array. Even though this is done in one Java statement, it does not happen all at once. By mak- ing each method synchronized, no thread using these operations can see an incorrect intermediate state. It effectively ensures there is some global order of calls to isEmpty , push , and pop . That order might differ across different executions because scheduling is nondeterministic, but the calls will not be interleaved.
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
  • Race condition, data races, bad interleavings

{[ 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