Using "this" as lock object

Jan 6, 2011 at 11:33 AM

Hi - on slide 32 in unit 2a, it says:

Designate a lock object (in your mind, and with comments)
Any object will do (can use this as well)

I'd strongly suggest to remove this clause; and maybe even explain why it is a bad practice to do it!
Reason: "this" it "the object around" - and in almost all cases this object is exposed to some client code, i.e. it is used like that:

    var foo = new Foo();
   
So now it is possible that the client code contains code like

    lock (foo) { ... }
   
on arbitrary threads. This will almost certainly kill the designed locking behavior.

(I'm not sure whether the "often" comment in the notes means the same (the word "often" does not appear on the slide)

Regards
Harald M.

Jan 11, 2011 at 9:33 PM

Harald,

Thanks for the comment: it is right on target. I've made the change. Best,

-- Tom