The deadlock in this situation occurs because the two threads are trying to lock both A and B before performing any of the functions. Wouldn't a simple fix (in this situation) be to this be to call withdraw or deposit only within 2 single (rather than nested) synchronization blocks? Are there downsides to this in terms of performance? I do see the value of TM, but I am curious about the example provided in this slide.
MangoSister
I think the point here to show that manually write lock-based code is not trivial in many cases. Although I agree that it is easy to fix this example, but the problem may get much more complicated in some other cases, where using TM will make your life much easier.
The deadlock in this situation occurs because the two threads are trying to lock both A and B before performing any of the functions. Wouldn't a simple fix (in this situation) be to this be to call
withdraw
ordeposit
only within 2 single (rather than nested) synchronization blocks? Are there downsides to this in terms of performance? I do see the value of TM, but I am curious about the example provided in this slide.I think the point here to show that manually write lock-based code is not trivial in many cases. Although I agree that it is easy to fix this example, but the problem may get much more complicated in some other cases, where using TM will make your life much easier.