[Mono-list] How to implement the monitor synchronization?

Serge serge@wildwestsoftware.com
Wed, 14 Nov 2001 02:05:57 +0200


> But wait a second! What happens when control is transfered out of the
> basic block. Of course the compiler inserts call to monitor exit just
> before method return, but what about when exceptions are thrown. The CLI
> needs to release monitors when unwinding?


Similarly to synchronized blocks in Java, C# has "lock" keyword. And just
like Java compiler generates exception table and corresponding handler that
releases lock and rethrows the exception, C# compiler generates try/finally
block for "lock" statement. Same is true for methods marked "synchronized"
(lock released by runtime in the case of exception).
On the other hand, if you explicitly use Monitor's methods it's up to you to
take care of possible exceptions.