Prev: SoundPlayer
Next: Error "ActiveX control '8856f961-340a-11d0-a96b-00c04fd705a2'cannot be instantiated because the current thread is not in a single-threaded apartment."
From: Tony Johansson on 20 Jun 2010 11:09 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 20 Jun 2010 11:49 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 20 Jun 2010 13:06 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 20 Jun 2010 14:45 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 20 Jun 2010 15:17
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 |