161115-SynchronizationPrimitives.pdf

This notify do dequeue no notification sending one

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

View Full Document Right Arrow Icon
this . notify (); ... do dequeue ... // no notification! Sending one notification on the first dequeue was fine at that point, but that still left a thread blocked. If the second dequeue happens before any enqueue, no notification will be sent, which leaves the second blocked thread waiting when it should not wait. The simplest solution in this case is to change the code to use notifyAll instead of no- tify . That way, whenever the buffer becomes non-empty, all blocked producers know about it and similarly whenever the buffer becomes non-full, all blocked consumers wake up. If there are n blocked threads and there are no subsequent operations like the second dequeue above, then n - 1 threads will simply block again, but at least this is correct. So this is correct : CPEN 221 – Fall 2016
Image of page 9

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 10 class Buffer<E> { // not shown: an array of fixed size for the queue with two indices // for the front and back, along with methods isEmpty() and isFull() void enqueue (E elt) { synchronized ( this ) { while ( isFull ()) this . wait (); ... do enqueue as normal ... if ( ... buffer was empty (i. e ., now has 1 element) ...) this . notifyAll (); } } E dequeue () { synchronized ( this ) { while ( isEmpty ()) this . wait (); E ans = ... do dequeue as normal ... if ( ... buffer was full (i. e ., now has room for 1 element) ...) this . notifyAll (); return ans; } } } If we do not expect very many blocked threads, this notifyAll solution is reasonable. Otherwise, it causes a lot of probably-wasteful waking up (just to have n - 1 threads probably block again). Another tempting solution almost works , but of course “al- most” means “wrong.” We could have every enqueue and dequeue call this.notify , not just if the buffer had been empty or full. (In terms of the code, just replace the if-statements with calls this.notify() .) In terms of the interleaving above, the first dequeue would wake up one blocked enqueuer and the second dequeue would wake up another one. If no thread is blocked, then the notify call is unnecessary but does no harm. The reason this is incorrect is subtle, but here is one scenario where the wrong thing happens. For simplicity, assume the size of the buffer is 1 (any size will do but larger sizes require more threads before there is a problem). 1. Assume the buffer starts empty. 2. Then two threads T 1 and T 2 block trying to dequeue. CPEN 221 – Fall 2016
Image of page 10
Reader/Writer Locks, Condition Variables and Other Synchronization Primitives 11 3. Then one thread T 3 enqueues (filling the buffer) and calls notify . Suppose this wakes up T 1 . 4. But before T 1 re-acquires the lock, another thread T 4 tries to enqueue and blocks (because the buffer is still full). 5. Now T 1 runs, emptying the buffer and calling notify , but in a stroke of miserable luck this wakes up T 2 instead of T 4 . 6. T 2 will see an empty buffer and wait again. So now T 2 and T 4 are blocked even though T 4 should enqueue and then T 2 could dequeue.
Image of page 11

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

View Full Document Right Arrow Icon
Image of page 12
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