From: Tony Johansson on
Hello!

According to the docs it says that lock (this) is a problem if the instance
can be accessed publicly.
Can somebody explain what can happen if I have a lock section and use this
as the lock object like this.
lock(this)
{

}

//Tony


From: Jeroen Mostert on
On 2010-06-20 17:09, Tony Johansson wrote:
> According to the docs it says that lock (this) is a problem if the instance
> can be accessed publicly.
> Can somebody explain what can happen if I have a lock section and use this
> as the lock object like this.
> lock(this)
> {
>
> }
>
http://haacked.com/archive/2006/08/08/threadingneverlockthisredux.aspx

--
J.
From: Arne Vajhøj on
On 20-06-2010 11:49, Jeroen Mostert wrote:
> On 2010-06-20 17:09, Tony Johansson wrote:
>> According to the docs it says that lock (this) is a problem if the
>> instance
>> can be accessed publicly.
>> Can somebody explain what can happen if I have a lock section and use
>> this
>> as the lock object like this.
>> lock(this)
>> {
>>
>> }
>>
> http://haacked.com/archive/2006/08/08/threadingneverlockthisredux.aspx

Quote:

"A while ago I wrote that you should never lock a value type ..."

I think that gives a good indication of the technical level
of the advice.

Something like on a scale from 1 to 10 around -2.

:-)

Arne


From: Peter Duniho on
Arne Vajhøj wrote:
> [...]
>> http://haacked.com/archive/2006/08/08/threadingneverlockthisredux.aspx
>
> Quote:
>
> "A while ago I wrote that you should never lock a value type ..."
>
> I think that gives a good indication of the technical level
> of the advice. [...]

Perhaps you could be more specific. The advice to never lock on a value
type is actually very good advice, and for the reason the author states
(value types are boxed, so you never get the same instance twice when
using "lock" or other Monitor methods).

Or put another way, how high could the quality of advice be when given
by someone who thinks locking on a value type is actually a good idea?

Pete
From: Arne Vajhøj on
On 20-06-2010 14:45, Peter Duniho wrote:
> Arne Vajhøj wrote:
>> [...]
>>> http://haacked.com/archive/2006/08/08/threadingneverlockthisredux.aspx
>>
>> Quote:
>>
>> "A while ago I wrote that you should never lock a value type ..."
>>
>> I think that gives a good indication of the technical level
>> of the advice. [...]
>
> Perhaps you could be more specific. The advice to never lock on a value
> type is actually very good advice,

Given that the C# compiler gives an error when attempting to
do it, then ...

Arne