Re: multi threading in C#
- From: "Peter Duniho" <NpOeStPeAdM@xxxxxxxxxxxxxxxx>
- Date: Thu, 24 May 2007 01:53:16 -0700
On Thu, 24 May 2007 00:21:44 -0700, Jon Skeet [C# MVP] <skeet@xxxxxxxxx> wrote:
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.
Okay, maybe it's just because I'm up too late, but I still not sure I get it.
Let's assume the Queue class is defined as not thread-safe. After all, if it were thread-safe you wouldn't have to do all this work yourself, right? But then, if the class is defined as not thread-safe, why would it be doing any locking itself?
And conversely, if it is doing locking itself, wouldn't it be thread-safe? (That is, at least nominally...not counting bugs in its implementation of thread-safeness, of course :) ). And so by implication, if it's doing locking, why would you be doing locking as well?
Interestingly (at least to me, anyway) at least part of the issue comes about because of the use of the Monitor class, which apparently has knowledge of the compiler keyword "lock". While I see the design simplicity of having the Monitor class, it does introduce some hiding of implementation details that produce less-than-intuitive results. In particular, the first time I saw code using Monitor, I had no idea why the code didn't deadlock, because I didn't understand that calling Wait() released the lock (all I saw was two different sections of code waiting on the same object, with no apparent releasing semantics).
But the other odd thing is that provides an avenue for code unrelated to the higher-level code that did the original lock to wind up unlocking the lock, unbeknownst to the code that locked the object in the first place. Of course, I suppose that's the "cost" of having a built-in locking mechanism, but because of the way "lock" appears in code, it's a little confusing that you can be within the block of code contained by the "lock" statement, and still not be locked.
Which is, I guess, your point about using a different object. That is, while from a design perspective it seems *extremely* unlikely that you'd have a class that required you to do locking, but which still wound up doing locking (or unlocking, in this case) itself that screwed you up, it is technically possible and thus the use of an independent object to do the locking instead.
So, after all that...I guess my understanding is that the use of the object is about what's technically possible, rather than what is likely. Belt and suspenders, and all that. Is that right?
Pete
.
- Follow-Ups:
- Re: multi threading in C#
- From: Jon Skeet [C# MVP]
- 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#
- From: Jon Skeet [C# MVP]
- Re: multi threading in C#
- Prev by Date: Docking / positioning the toolbox in Visual Studio
- Next by Date: Re: How to replace the "\\" char with "\"
- Previous by thread: Re: multi threading in C#
- Next by thread: Re: multi threading in C#
- Index(es):
Relevant Pages
|