Read / write confusion lock

I'm not quite sure how best to execute this multi-threaded script, so any input would be appreciated.

I have one block that reads data so that multiple threads can access at once. I have one more block that writes data, only one thread can write at any time. Also, it cannot start writing while any thread is reading data. Is there a ReaderWriterLockSlimway to go here, will it wait until the read streams are complete before blocking the stream for writing?

+5
source share
2 answers

Yes, ReaderWriterLockSlimideal for frequent read / infrequent scripts.

The behavior, you guessed it, is only one writer, writers are blocked until all readers are completed, readers canโ€™t access while the writer is in the process.

Be careful that the lock time (for reading or writing) is long enough to prevent concurrent access, and is no longer needed.

+5
source

Yes, it looks like ReaderWriterLockSlim is what you want.

A write lock will not be obtained until read locks are executed. I suggest you read the documentation for a full description of the behavior (blocking queues, etc.):

http://msdn.microsoft.com/en-us/library/system.threading.readerwriterlockslim.aspx

+1

All Articles