0
Reply

The Monitor class

bram 0

bram 0

May 7 2004 10:12 AM
1.8k
I have some problems understanding the exact quirks of the Monitor class. What is the difference between the Wait/Pulse method of acquiring and releasing a lock and the Enter/Exit method?! I read that the following: [code] class A { private int counter; public void Hit() { lock(this) { counter++; } } [/code] was susceptible to errors, since two different threads could be executing the [b]counter++[/b] statement at the same time. I don't see how this could be the case, since in my understanding the second thread would wait for the lock on 'this' to be released. Therefore, the access to the [b]counter++[/b] statement would be synchronized. Probably I'm wrong but I don't see how... Any ideas would be greatly appreciated!