----- Original Message -----
From: "Jekke Bladt" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Wednesday, September 29, 2004 9:11 PM
Subject: [ADVANCED-DOTNET] ReadWrite locks and how they work


> I'm having a problem with the ReadWriteLock object in .net and wondering
> if I'm misunderstanding the concept:
>
> In order to minimize blocking, I've written the class to copy the queue
> into an array, reset the queue, and release the reader lock. There is no
> place anywhere else that the queue is reinitialized or anything is
> dequeued.
>
> And yet, I am still having a problem where, during amsg = mQueue.ToArray
> is failing because the queue is empty. FileSave is never called with the
> queue empty.
>
> I suspect there's some conceptual error here, but I'll be damned if I
> can figure it out on my own. Can anyone help?
>
>    Protected Sub FileSave()
>        'Lock all threads out of the queue while we copy it.
>        mWRLQueue.AcquireReaderLock(miQueueReadWriteTimeout)
>        'Copy the message queue to an array.
>        Dim amsg() As Object
>        If mQueue.Count > 0 Then
>            amsg = mQueue.ToArray()
>            'Clear the queue
>            mQueue = New LogMessageQueue

when your reader modifies data, it de facto becomes a writer and it should
be given exclusive access to data (via writer lock). "Readers" should only
read data, never modify data. "Writer" is someone that should get exclusive
access to data in order to modify the data. It is possible that two threads
check queue length, then one of them succeeds to copy & clear the queue -
the second one will find the queue empty.

Let's take a disctionary as an example. A number of readers can
simultaneously access the dictionary in order to check if a given word
exists or to compute some statistics (e.g., the number of words shorter than
3 letters). They do not modify the dictionary. From time to time, a writer
gets an exclusive access to the dictionary in order to add a new word or
remove an incorrect one; thus modifying the data.

Marek

===================================
This list is hosted by DevelopMentorŪ  http://www.develop.com
Some .NET courses you may be interested in:

Essential .NET: building applications and components with CSharp
August 30 - September 3, in Los Angeles
http://www.develop.com/courses/edotnet

View archives and manage your subscription(s) at http://discuss.develop.com

Reply via email to