Re: multi threading in C#



On May 24, 7:42 am, "Peter Duniho" <NpOeStPe...@xxxxxxxxxxxxxxxx>
wrote:
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.

Only the ProducerConsumer class has access to listLock. The general
public (as it were) doesn't have access to the queue reference - but
the queue itself does. If it chose to lock on itself and call
Monitor.Wait(this) for instance, that could screw up the signalling in
the rest of the code. I think it's highly unlikely that it would do
that, but it's a possibility.

(It doesn't help that there are two variables called queue here - I'm
talking about the one inside ProducerConsumer, not the one referring
to ProducerConsumer itself!)

I also like it from an encapsulation point of view - it makes it easy
to replace listLock with a different type of locking type, eg the one
at http://www.yoda.arachsys.com/csharp/miscutil/usage/locking.html

Fundamentally, I don't think that you should be able to lock/wait/
pulse/etc on any old object reference - separating the lock from the
queue makes "my" world closer to my ideal world :)

(I wonder whether there's some way of encapsulating both parts in one
variable neatly with generics though... I'll have to think about
that.)

Jon

.



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)
  • [PATCH 05/19] io-controller: Common hierarchical fair queuing code in elevaotor layer
    ... Requests keep a reference on ioq and ioq keeps keep a reference ... So the queue can be freed. ... Cgroup deletion path holds iocg->lock and removes iog entry ...
    (Linux-Kernel)
  • [PATCH 05/20] io-controller: Common hierarchical fair queuing code in elevaotor layer
    ... Requests keep a reference on ioq and ioq keeps keep a reference ... So the queue can be freed. ... Cgroup deletion path holds iocg->lock and removes iog entry ...
    (Linux-Kernel)
  • [PATCH 09/25] io-controller: Common hierarchical fair queuing code in elevaotor layer
    ... Requests keep a reference on ioq and ioq keeps keep a reference ... So the queue can be freed. ... Cgroup deletion path holds iocg->lock and removes iog entry ... struct io_entity *entity) ...
    (Linux-Kernel)
  • Re: 7.0 wishlist?
    ... doing it at compile time runs into the problems shown. ... At least an NPE in seemingly-null-safe could would then be prima facie evidence of thread-unsafety in the null's source chain, and with a runtime check on the reference assignment the NPE will occur in one of blocks that participated in a race condition instead of somewhere random later on. ... void method ... For that, I have the thread hold the queue in a WeakReferenceand if the removemethod hits the timeout, it sleeps for a while holding no other references to the queue. ...
    (comp.lang.java.programmer)