Lec30-Classical Synchronization Problems-RW

The naïve approach is to allow only one thread to

This preview shows 3 out of 7 pages.

The naïve approach is to allow only one thread to access the database at a time But multiple readers can access at a time
Image of page 3

Subscribe to view the full document.

4 State variables: # of active readers -- AR = 0 # of active writers -- AW = 0 # of waiting readers -- WR = 0 # of waiting writers -- WW = 0 Condition okToRead Condition okToWrite Lock lock Acquire lock as soon as you enter the critical section Release lock before leaving the critical section If you realize that you cannot proceed inside the critical code Wait
Image of page 4
5 Reader Initial status is waiting wait until no writers Change status to active reader access database No more active check out -- wake up waiting writer Writer Initial status is waiting writer wait until no readers or writers Change status to active writer access database No more active check out -- wake up waiting readers or writer
Image of page 5

Subscribe to view the full document.

6 Reader() { lock.Acquire(); WR++; while (AW > 0) { // check if safe to read // if any writers, wait okToRead.Wait(&lock); } WR--; AR++; lock.Release(); Access DB lock.Acquire(); AR--; if (AR == 0)//if no other readers still // active, wake up writer okToWrite.Signal(&lock); lock.Release(); }
Image of page 6
7 Writer() { // symmetrical lock.Acquire(); WW++; while ((AW + AR) > 0)// check if safe to write // if any readers or writers,wait okToWrite->Wait(&lock); WW--; AW++; lock.Release(); Access DB // check out lock.Acquire(); AW--; okToRead->Broadcast(&lock); okToWrite->Signal(&lock); lock.Release(); }
Image of page 7
You've reached the end of this preview.

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