'this' is a special keyword which refers to the instance of the class
which is executing the instance code. You can't expose 'this' since
the client already has a reference to the object anyway. So its not
'exposing implementation detail', since the client code has no way of
knowing what you're really locking on (short of decompling your
assembly).
Its true you can have deadlocks within your code without locking on
this; I never said that you couldn't. The point is that you have
control over your own class, and thus know exactly what you're locking
on and can fix it.
What you may not have control over is the client code using your class;
that code may decide to lock on your instance of the object, then call
a method which locks on 'this' That will create a deadlock. This is
especially bad if your code happens to be a library you're selling,
because it will seem as if your library is broken.
You don't have to take my word for it though, this is straight from
MSDN:
"Generally, it is best to avoid locking on a public type, or on object
instances beyond the control of your application. For example,
lock(this) can be problematic if the instance can be accessed publicly,
because code beyond your control may lock on the object as well. This
could create deadlock situations where two or more threads wait for the
release of the same object. Locking on a public data type, as opposed
to an object, can cause problems for the same reason. Locking on
literal strings is especially risky because literal strings are
interned by the common language runtime (CLR). This means that there is
one instance of any given string literal for the entire program, the
exact same object represents the literal in all running application
domains, on all threads. As a result, a lock placed on a string with
the same contents anywhere in the application process locks all
instances of that string in the application. As a result, it is best to
lock a private or protected member that is not interned."
ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.VisualStudio.v80.en/dv_csref/html/413e1f28-a2c5-4eec-8338-aa43e7982ff4.htm
No where does it say that locking on this is bad because it exposes
implementation detail (it doesn't, unless the person using your code
figures out why calling certain methods is deadlocking their
application).
Also, please do NOT send me email replies; keep all replies to me on
the thread only.
Thanks
Andy
Nicholas Paldino [.NET/C# MVP] wrote:
That's not really an accurate way of indicating why you don't lock on
"this".
While it is true that exposing "this" can contribute to deadlocks, it's
a very small reason. Deadlocks are more an issue that arises when there is
a coupling between objects and the order of operations is not defined
clearly. Deadlocks can certainly arise with using internal objects which
are private as locks. It's not an issue specific to exposing "this".
Ultimately, the reason you do not want to expose "this" is that it is
exposing an implementation detail, which is private. Just like you don't
expose fields which are the backing for properties, you don't expose this.