81530

What thread-safe collection is appropriate for this scenario?

Question:

I have a singleton class handling Subscribe and Unsubscribe requests from different clients running in different threads. The singleton contains a collection of Subscribers. Before adding or removing, the class needs to validate that the element hasn't already been added or deleted.

Normally I would implement this using a List or a HashSet calling Contains before each operation, but if I want to use one of the new classes in the System.Collections.Concurrent namespace, the only option I see is to use a ConcurrentDictionary with a dummy value.

Is this my best option or is there something I'm overlooking? By the way, performance is not really a factor to consider.

Answer1:

I think you should stick with <a href="http://msdn.microsoft.com/en-us/library/dd287191.aspx" rel="nofollow">ConcurrentDictionary</a> ...

Answer2:

Why dont you use ConcurrentDictionary backed with Blocking Queue. BlockingQueue seems to fit your producer/consumer scheme, and ConcurrentDictionary for your lookups.

Recommend

  • Can't retrieve user roles
  • Return value of smallest index of repeated number in array
  • How to tell Dozer to use LinkedHashSet collection in destination field?
  • Neo4j how to delete nodes recursively from some start node
  • General contract for object comparision : equals() and hashCode()
  • Variable might not have been initialized
  • ValidationResult Returned From IValidatableObject.Validate Is Not Localized
  • iOS Validation failed -> Bundle is invalid (Xcode 6.2, iOS 8.2)
  • Complex Object won't return when using 'include' syntax in WCF with Entity Version 6
  • jQuery - how to validate a date of birth using jQuery Validation plugin?
  • 'doc_del_count' bigger than 'doc_count' on CouchDB
  • iOS Localization Doesn't Work with More Than 63 Files
  • Efficient algorithm to find additions and removals from 2 collections
  • What is the default HTTP verb in WebApi ? GET or POST?
  • why calling cd shell command through system() or execvp() from a child process won't work?
  • How can I get the choice “H2” back in the H2 consol?
  • Upload file that is in the cpan database
  • TFS 2015 - Waiting for an agent to be requested
  • SonarQube: Cannot deactivate rule with missing quality profile
  • How to synchronize jQuery dialog box to act like alert() of Javascript
  • How do I include a SWC in an AS2 Flash project?
  • How to add a focus style to an editable ComboBox in WPF
  • Remove changes from one element when event occurs on another element?
  • Object and struct member access and address offset calculation
  • How do I superscript characters in a UIButton?
  • C: Incompatible pointer type initializing
  • why xml file does not aligned properly after append the string in beginning and end of the file usin
  • Remove final comma from string in vb.net
  • How to define and use opencv mat of user type
  • QLineEdit password safety
  • Is my CUDA kernel really runs on device or is being mistekenly executed by host in emulation?
  • Validaiting emails with Net.Mail MailAddress
  • Cassandra Data Model
  • Weird JavaScript statement, what does it mean?
  • Why winpcap requires both .lib and .dll to run?
  • How do I rollback to a specific git commit
  • Getting Messege Twice Using IMvxMessenger
  • Django query for large number of relationships
  • Python/Django TangoWithDjango Models and Databases
  • git trying to push non-existent file … after clearing cache