Re: Threads - why isn't a whole object locked when ...?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



On Sat, 24 Jan 2009 18:11:47 -0800, <beginwithl@xxxxxxxxx> wrote:

Uh, I don’t get it. I’m prob way off with this --> You mean if I have
a public method A_m1 ( in object A1 ) and I use “this” reference for a
lock inside A_m1, then outside code may at the same time use a
reference to A1 on a lock inside some other method ( which may very-
well be defined in some other class )?
If that is what you guys meant ( which I highly doubt ), then why
would that cause a dead-lock?

That's exactly what I meant. As Arne says, it's not guaranteed to cause dead-lock. You still need another lock, taken in a different sequence from the code using the "this" reference as the locking object. The point is that if you keep your locking object private, then you _know_ no other code is going to try to use it for locking.

Doing that doesn't even guarantee there won't be dead-lock. If there's poorly written code elsewhere, there's always the possibility they'll find some other way to create a dead-lock. It's just that using the "this" reference means you're basically leaking part of your synchronization implementation, and it's never a good idea to leak implementation. In this case, leaking implementation makes it a lot easier for dead-lock to occur.

[...]
Which object you use for locking has nothing to do with what you protect
with the lock statement.

I thought it does in cases where object used for locking is also
object the code inside the lock statement is operating on

Nope. There's nothing special about the "this" reference versus any other reference. It's used exactly in the same way.

(Note: that's not strictly speaking true. In particular, there are some places where the "this" reference gets used implicitly for locking -- such as default event implementations and there's a code attribute that synchronizes an entire method and which uses "this" -- but even those cases still wind up implicitly using "this" in the same way that any other reference would be used).

[...]
No, that doesn't really make sense as you can't lock an object. If you
pass some data into a method that uses a lock, it's up to the method to
protect that data also.

What would be the best way to protect that data? If you pass some
object O of type T into a method, then only way you can protect O
( while code in a method is operating on it), is that you somehow make
sure there is no other method ( defined in this or any other class )
operating on that data at the same time.?!
Now since you can’t lock O, I’m not sure how you would manage to
protect it ( assuming some third party created type T and also
assuming app also has some third-party objects that also can operate
on O )?!

You can only protect data in the same cooperative way any synchronization occurs. Don't read too much into the word "protect" in this context. The data is protected only insofaras you correctly synchronize all access to the data.

Pete
.



Relevant Pages

  • Re: Thread.Monitor & references
    ... >I need to make access to a reference object threadsafe. ... you don't need to protect it with Monitor.Enter/Exit ... since I have no other pointers to this thing. ... Is there any way to use the slick Monitor (or lock) syntax ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Threads - why isnt a whole object locked when ...?
    ... lock and cause a deadlock situation. ... As for the question about using an internal reference instead of the ... Which object you use for locking has nothing to do with what you protect ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: [PATCH 2.6.28-rc5 01/11] kmemleak: Add the base support
    ... It traces the memory allocation/freeing in a way similar to ... For some RCU implementations, but only by accident. ... search tree and removed in delete_objectwith this lock held ... OK, holding an object's lock can protect that object from deletion, ...
    (Linux-Kernel)
  • Re: [PATCH 2.6.28-rc5 01/11] kmemleak: Add the base support
    ... It traces the memory allocation/freeing in a way similar to ... search tree and removed in delete_objectwith this lock held ... OK, holding an object's lock can protect that object from deletion, ...
    (Linux-Kernel)
  • Re: [PATCH 2.6.28-rc5 01/11] kmemleak: Add the base support
    ... It traces the memory allocation/freeing in a way similar to ... search tree and removed in delete_objectwith this lock held ... Freeing a memleak_object is done via an RCU callback invoked from ... What exactly does object_tree_lock protect? ...
    (Linux-Kernel)