MorningLightMountain

a little something from an immotile

Posts Tagged ‘locks’

Never lock another mans Type

Posted by Dan G on November 11, 2011

Something I’ve come across a lot of in the past is use of the lock statements on publicly exposed things (types, instances) :

lock(this) {
lock(typeof(MyTypeWithALock)) {


Both examples are bad for the same reason – the thing you’re locking on is public. So anyone within your app domain can lock on the same thing, deadlock city looms. If it’s your lock, keep it private, it doesn’t take long to put in a field, either instance or static, and it only has to be a simple object. Get into the habit and avoid a future “WTF is going on?” moment or two.

Bonus point : this answer on Stack Overflow explains an extra definition of public – between appdomains in the same process.

Posted in Good practie | Tagged: , | Leave a Comment »

 
Follow

Get every new post delivered to your Inbox.

Join 165 other followers