Subj : Re: Improving read-write lock To : comp.programming.threads From : Alexander Terekhov Date : Thu Feb 24 2005 03:00 pm David Holmes wrote: > > "Joe Seigh" wrote in message > news:opsmd8w7vwqm36vk@grunion... > > IIRC, Java exceptions don't unlock monitor locks. Of course Java doesn't Java's "synchronized" thing is kinda RAII for monitor locks. > > use dtors for scoping since it has GC. Java's silly approach for scoping and deterministic cleanup apart from monitor locks is try/finally. C# does sorta improve it a bit with "using" and IDisposable.Dispose(). ---- A using statement of the form using (R r1 = new R()) { r1.F(); } is precisely equivalent to r1 = new R(); try { r1.F(); } finally { if (r1 != null) ((IDisposable)r1).Dispose(); } A resource-acquisition may acquire multiple resources of a given type. This is equivalent to nested using statements. A using statement of the form using (R r1 = new R(), r2 = new R()) { r1.F(); r2.F(); } is precisely equivalent to: using (R r1 = new R()) using (R r2 = new R()) { r1.F(); r2.F(); } which is, by expansion, precisely equivalent to: R r1 = new R(); try { 33 R r2 = new R(); try { r1.F(); r2.F(); } finally { if (r2 != null) ((IDisposable)r2).Dispose(); } } finally { if (r1 != null) ((IDisposable)r1).Dispose(); } ---- > > > > Java monitor locks are automatically unlocked when an exception propagates > out of a synchronized block or method. I think the C++ folk popularised this > peculiar notion with RAII for locking. :( Brain-dead current semantics of exceptions specs aside for a moment, C++ doesn't require unwinding for unexpected exceptions and sensible implementations do not unwind; they just call terminate() at throw point. So C++ RAII (for locking and other stuff) isn't that bad. It's certainly better in this respect than brain-dead Java/C#'s "finally" that "catch" everything. regards, alexander. .