The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

import-bot (20211) [Avatar] Offline
#1
[Originally posted by pdavila]


Mr. Dennis,

Great book. I especially like the e-book format.

I have a question that troubles me.

I'm writing a C# application. I have a large array of Objects, about 300000
elements, that is the source of data for 5 other classes. Each one of the 5
classes essentially builds a different forest of balanced binary trees from
the data.

The data array is written to only once, before any of the 5 Tree classes
are created. My question is, if the 5 classes are only reading the source
array and never writing to it, do I still need to use a ReaderWriterLock
object and get Read locks before each one of the classes reads from the array.

Thanks in advance,

Peter
import-bot (20211) [Avatar] Offline
#2
Re: ReaderWriterLock
[Originally posted by pdavila]


Alan,

Thanks for your comments.

The part that is confusing, is that the .NET documentation specifically
singles out the ArrayList as being thread safe for reading. It makes me
suspect other collection classes as not been thread safe for reading. In other
words, would an array, or a queue, for example, be thread safe if all I'm
doing is reading the data from multiple threads?

P.S. I do understand your comment about state information. Fortunately,
the state information is encapsulated within and with each class that contains
each thread. Each thread uses it own state information.

Thanks,

Peter
import-bot (20211) [Avatar] Offline
#3
Re: ReaderWriterLock
[Originally posted by pdavila]


I'm starting to understand this threading stuff.

I can see where the Queue example that I gave before is not really a good
one, since the queue will not be thread safe for reading from multiple
threads. A thread could conceivably read a value and be interrupted, before
the queue is properly made ready for another thread read.

But, isn't it correct to assume that a fixed sixed array, e.g.,

Object arrayObjects = new Object[100000];

would be perfectly safe to read from multiple threads, as long as each
thread keeps track of the index that it is handling at any given time, and as
long as none of the threads write to it?

By the way, here are 2 Manning books that I could use. One on DirectX and
one on the Media 9 API/SDK and implications with .NET code.

Peter
import-bot (20211) [Avatar] Offline
#4
Re: ReaderWriterLock
[Originally posted by pdavila]


Ooops,

I should have mentioned in my post that the 5 classes manipulating the data
each have a thread running to do so. In other words, I need the array to be
thread-safe for reading, since no writing is done by the time the 5 threads
look at the data.

In your book, you metioned that an ArrayList is thread safe for reading.
The question then is, is a regular run-of-the-mill, fixed sized array, thread
safe for reading?

Thanks,

Peter
import-bot (20211) [Avatar] Offline
#5
Re: ReaderWriterLock
[Originally posted by alan]

If the data is only being read and the objects being read don't persist any
state information (such as current node) I can't think of any reason you would
need a synchronization mechanism.

Hope this helps,
Alan