Re: multi threading in C#

Tech-Archive recommends: Speed Up your PC by fixing your registry



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
.



Relevant Pages

  • Re: [PATCH] 3/3 maple: update bus driver to support Dreamcast VMU
    ... Removes unneeded locking of queue of processed packets while properly ... Under what conditions will this lock be ...
    (Linux-Kernel)
  • Re: using clustered index to optimize inserts ...
    ... I will try to explain locking in terms of Sybase docs... ... Allpages Locking: Allpages locking locks both data pages and index ... the data page is locked with an exclusive lock. ... Clustered Index: The datarows will be arranged as per the clustered ...
    (comp.databases.sybase)
  • Re: location of bioq lock
    ... > to work on the queue, ... > So we need to know where the lock is. ... Each bioq is owned by the consumer of it, i.e. the individual driver. ... locking it is the responsibility of the driver. ...
    (freebsd-current)
  • Re: CSingleLock - known behaviour?
    ... It is better to design code that doesn't require locking. ... If you don't need the resource, don't lock it. ... magnitude less efficient, than locking once. ...
    (microsoft.public.vc.mfc)
  • Re: Strange multi-user timing phenomenon
    ... table record with pessimistic locking and both edit the same record. ... the recordset will be sufficient to lock out other users. ... update the recordset (if you had to edit data) and then close it. ...
    (microsoft.public.access.formscoding)