Re: multi threading in C#



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

It sounds like you want a producer/consumer queue, or something like
it. See the second half of this page:

http://pobox.com/~skeet/csharp/threads/deadlocks.shtml


By the way, as long as we're on the topic, there's been something I've been curious about for awhile. I see in your example code, and I've seen this elsewhere as well, the creation of a new, base object instance for use as a locking object. I am curious what the point of that is, as compared to simply acquiring a lock on the object you are trying to synchronize access to.

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

The only thing that comes to mind is that in some cases, you may have an object where only certain parts of it need to be synchronized, but those parts may still be more than a single object within the larger object. In that case, having a dedicated object instance for locking allows for separation of the locking semantics from the actual implementation.

But otherwise? Are there other good reasons for doing it that way, or is it just a matter of being consistent since you would need do it that way sometimes?

Thanks,
Pete
.



Relevant Pages

  • 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: 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)
  • Re: Strange multi-user timing phenomenon
    ... I tried your locking method using a perssimistic recordset and it appears to ... user places his lock and grabs the same record. ... I would try opening a fixed single-row recordset with pessimistic locking, ...
    (microsoft.public.access.formscoding)
  • Re: CMultiLock example
    ... Seriously, though, locking is *mandatory* if two or more threads are accessing non-scalar ... lists ever be modified while the threads are running. ... without a lock. ... You must prevent that one piece of data is modified from multiple ...
    (microsoft.public.vc.mfc)

Loading