Re: multi threading in C#
- From: "Jon Skeet [C# MVP]" <skeet@xxxxxxxxx>
- Date: 24 May 2007 00:21:44 -0700
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
.
- Follow-Ups:
- Re: multi threading in C#
- From: Chris Mullins [MVP]
- Re: multi threading in C#
- From: Peter Duniho
- Re: multi threading in C#
- From: Moty Michaely
- Re: multi threading in C#
- References:
- Re: multi threading in C#
- From: Jon Skeet [C# MVP]
- Re: multi threading in C#
- From: Peter Duniho
- Re: multi threading in C#
- From: Jon Skeet [C# MVP]
- Re: multi threading in C#
- From: Peter Duniho
- Re: multi threading in C#
- Prev by Date: Re: How do I stop my software from getting cracked?
- Next by Date: Re: Synchronizing asynchronous HttpWebRequest
- Previous by thread: Re: multi threading in C#
- Next by thread: Re: multi threading in C#
- Index(es):
Relevant Pages
|