0

I have 1.N threads writing to a .net 4.0 ConcurrentDictionary. I have another thread which fires every 5 seconds (settable in app.config) to enumerate the dictionary, and potentially remove some items. I want to halt the write operations during the enumeration.

What would be the best primitive to synchronize these two operations. Any help would be appreciated.

Bob.

1 Answer 1

2

use ReaderWriterLockSlim (http://msdn.microsoft.com/library/system.threading.readerwriterlockslim(VS.90).aspx) so enumerating thread will request exclusive lock and writing threads non-exclusive.

Sign up to request clarification or add additional context in comments.

4 Comments

It doesn't seem to be stopping the updates to the collection. It seems to be ceeding control back from the enumeration thread meaning the enumeration thread isn't seening the full dictionary. The locks being released during the enumeration.
are you sure you followed my advice correctly? enumeration must require exclusive lock, this means that it can't be interrupted
Yip. but i've coded around it. I thought it would work, as I thought ReaderWriterLockSlim for sync would be the first choice before I posted the question.
Andrey, I'll dig it out and post it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.