Re: multi threading in C#



On Wed, 23 May 2007 23:27:21 -0700, Jon Skeet [C# MVP] <skeet@xxxxxxxxx> wrote:

For example, instead of using "listLock" for the lock() and Monitor
methods, why not just use the "queue" instance?

If you lock on a reference that only you have access to, that
guarantees that no-one else can screw things up by locking on it
themselves.

Okay, the way I read that is that in the "listLock" versus "queue" reference example, "listLock" is the reference only you have access to, and the implication is that "queue" is a reference that others have access to.

Uh. Maybe I'm missing something, but...if you lock on a reference that only you have access to, but the object you're protecting is visible to others, doesn't that also mean that an alternate way for someone else to screw things up is by accessing the object *without* locking it?

It seems to me that if you've got an object that needs locking, and it somehow becomes accessible to outside code not guaranteed to obey the correct locking semantics, you've got problems one way or the other. Either the outside code locks it when you don't want it to, or it doesn't lock it when it ought to.

Isn't the correct solution to actually hide the object that needs locking from other code? That way the other code can't screw with it one way or the other.

I feel like something just went over my head, but I'm not sure what it is.

Pete
.



Relevant Pages

  • cvs-src summary for June 7-14
    ... Here's the summary - hope you like locking. ... You can get old summaries, and an HTML version of this one, at ... Poul-Henning Kamp committed reference counting for the tty code; ...
    (freebsd-current)
  • Re: Reeves Drive slipping
    ... There are 2 screws in the handle that are used to adjust the locking ... One screw locks the adjusting screw. ... the locking tension must be so tight that it requires great ...
    (rec.crafts.woodturning)
  • Re: Threads - why isnt a whole object locked when ...?
    ... All of your questions are answerable by pointing out that you have an incorrect mental model of what it means to "lock" something. ... That is, while it's entirely sensible to people who are familiar with standard thread synchronization techniques, in reality it's not actually locking anything. ... Instead, it's using the reference as a sort of traffic signal for other threads, which are cooperating, to respect. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Damn you, FEDEX! or Nikon D40 lost in Springfield, MO blackhole.
    ... the 2 mp Mavica he had been using with a Nikon D40. ... After shopping around, he got me to order one for him. ... The shipper had it insured, but from what I have read it could take weeks to sort this crap out. ... You may get your insurance from FedEx and a couple weeks later they find it and deliver it. ...
    (alt.photography)
  • Re: [PATCH] reiserfs: Expand i_mutex to enclose lookup_one_len
    ... version of the code where I kept a reference to /.reiserfs_priv/xattrs. ... Then I decided that .reiserfs_priv was all I needed to cache (to avoid ... recursive i_mutex locking on the fs root) and dropped the caching of ...
    (Linux-Kernel)