
Signature
Göran Andersson
_____
http://www.guffa.com
> Also:
>
> You don't lock the object, you lock the code. The lock prevents another
> thread to enter a code block that is locked on the same object. The
> object that you use in the lock statement is just an identifier for the
> lock, it doesn't protect the object.
While I see your point, I'd say you lock the *reference* rather than
either the object or the code.
In particular, the phrase "lock the code" (without reading the rest of
the text) would suggest that no other thread can execute the same code
at the same time. It can, if it ends up locking on a different
reference.
The explanation is spot on, of course.

Signature
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Göran Andersson - 11 Sep 2007 21:04 GMT
>> Also:
>>
[quoted text clipped - 10 lines]
> at the same time. It can, if it ends up locking on a different
> reference.
I see what you mean. I'd say that it doesn't lock the reference either,
it just locks the lock. :)
> The explanation is spot on, of course.
Thanks. :)

Signature
Göran Andersson
_____
http://www.guffa.com
Peter Duniho - 11 Sep 2007 21:12 GMT
>>> Also:
>>>
[quoted text clipped - 7 lines]
> I see what you mean. I'd say that it doesn't lock the reference either,
> it just locks the lock. :)
I think what you call it is not nearly so important as understanding
that like a critical section or other synchronization mechanism, it only
protects data when all code that accesses the data cooperates and uses
the same mechanism.
Code isn't locked, the object isn't locked, nothing is literally locked.
What does happen is that the synchronization mechanism in use ensures
only a single thread at a time has access to the resource.
Without cooperation between all of the relevant code, this doesn't happen.
IMHO, your original elaboration made this point reasonably well. :)
There's probably no need to worry about whether we are saying that the
variable is locked or the code is locked or the object is locked or
the... :)
Pete