Tuesday, May 03, 2005

Not lock() on this

I started reading more of what Jon had written, and, among a load of very interesting stuff, I found this small gem:

"Many books and articles recommend locking on this for instance methods and typeof(MyTypeName) (the Type object for whatever type you're writing code in) for static methods. I believe this is a bad idea, because it means you have less control over your locks. Other code may well end up locking on the same object as you do within your code, which makes it far harder to ensure that you only obtain locks in an appropriate order."

I had never thought about this earlier, perhaps because I have not done much serious thread work before ;)

No comments: