This situation is like someone leaving the room

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

This situation is like someone leaving the room through a window without removing the do-not-disturb sign from the door, so nobody else ever enters the room. A fix in this case would be to add lk.release in the branch of the if-statement before the throw-statement. But more generally we have to worry about any exception that might be thrown, even while some other method called from within the critical section is executing or due to a null-pointer or array-bounds violation. So more generally it would be safer to use a catch-statement or finally-statement to make sure the lock is always released. Fortunately, when we use Java’s locks instead of our pseudocode locks, this will not be necessary. A second problem, also not an issue in Java, is more subtle. Notice that withdraw calls getBalance and setBalance while holding the lock for the account. These methods might also be called directly from outside the bank-account implementation, assuming they are public. Therefore, they should also acquire and release the lock before access- ing the account’s fields like balance . But if withdraw calls these helper methods while holding the lock , then as we have described it, the thread would block forever — waiting for some thread to release the lock when in fact the thread itself already holds the lock. There are two solutions to this problem. The first solution is more difficult for program- mers: we can define two versions of getBalance and setBalance . One would acquire the lock and one would assume the caller already holds it. The latter could be private, perhaps, though there are any number of ways to manually keep track in the program- mer’s head (and hopefully documentation!) what locks the program holds where. The second solution is to change our definition of the acquire and release operations so that it is okay for a thread to (re)acquire a lock it already holds. Locks that support this are called reentrant locks . (The name fits well with our do-not-disturb analogy.) To define reentrant locks, we need to adjust the definitions for the three operations by adding a count to track how many times the current holder has reacquired the lock (as well as the identity of the current holder): CPEN 221 – Fall 2016
Image of page 9

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

Basic Shared-Memory Concurrency 10 new creates a new lock with no current holder and a count of 0 acquire blocks if there is a current holder different from the thread calling it . Else if the current holder is the thread calling it, do not block and increment the counter. Else there is no current holder, so set the current holder to the calling thread. release only releases the lock (sets the current holder to “none”) if the count is 0. Otherwise, it decrements the count. In other words, a lock is released when the number of release operations by the hold- ing thread equals the number of acquire operations.
Image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.
  • Fall '17
  • satish
  • Subroutine, Concurrent computing, Basic Shared-Memory Concurrency

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