views:

103

answers:

1

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.

A: 

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.

Andrey
It doesn't work, unfortunately.
scope_creep
why? please explain
Andrey
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.
scope_creep
are you sure you followed my advice correctly? enumeration must require exclusive lock, this means that it can't be interrupted
Andrey
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.
scope_creep
show your code, i still have a feeling that smthing is wrong
Andrey
Andrey, I'll dig it out and post it.
scope_creep