1

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.

Mike
  • 753
  • 1
  • 7
  • 16

2 Answers2

1

I think you should stick with ConcurrentDictionary ...

Yahia
  • 69,653
  • 9
  • 115
  • 144
  • "and unlike sets, bags support duplicates." vs. "Before adding or removing, the class needs to validate that the element hasn't already been added or deleted." – CodesInChaos Oct 25 '11 at 21:01
0

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

DarthVader
  • 52,984
  • 76
  • 209
  • 300
  • I'm not sure my scenario fits a producer/consumer scheme. Unsubscribe requests don't need to follow the same order as the Subscribe requests so I'm failing to see what the benefit would be of using a queue in this case. I don't see the added benefit compared to just using a dictionary. – Mike Oct 26 '11 at 13:45