Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ConcurrentDictionary's optimistically concurrent Remove method

I was looking for a method in ConcurrentDictionary that allows me to remove an entry by key, if and only if the value is equal to one that I specify, something like the equivalent of TryUpdate, but for removals.

The only method that does this seems to be this method:

ICollection<KeyValuePair<K, V>>.Remove(KeyValuePair<K, V> keyValuePair)

It is the explicit implementation of the ICollection interface, in other words, I have to cast my ConcurrentDictionary to an ICollection first so that I can call Remove.

Remove does exactly what I want, and that cast is no big deal either, also the source code shows it calls the private method TryRemovalInternal with bool matchValue = true, so it all looks nice and clean.

What worries me a bit however is the fact that it is not documented as the optimistically concurrent Remove method of ConcurrentDictionary, so http://msdn.microsoft.com/en-us/library/dd287153.aspx just duplicates the ICollection boilerplate, and the How to: Add and Remove Items from a ConcurrentDictionary does not mention that method either.

Does anyone know if that's the way to go, or is there some other method that I'm missing?

like image 253
Evgeniy Berezovsky Avatar asked Jan 04 '12 07:01

Evgeniy Berezovsky


People also ask

How ConcurrentDictionary works?

ConcurrentDictionary is thread-safe collection class to store key/value pairs. It internally uses locking to provide you a thread-safe class. It provides different methods as compared to Dictionary class. We can use TryAdd, TryUpdate, TryRemove, and TryGetValue to do CRUD operations on ConcurrentDictionary.

Is ConcurrentDictionary AddOrUpdate thread-safe?

It is thread safe in your usage. It becomes not thread safe when the delegate passed to AddOrUpdate has side effects, because those side effects may be executed twice for the same key and existing value.

What is the purpose of the ConcurrentDictionary TKey TValue class?

Represents a thread-safe collection of key/value pairs that can be accessed by multiple threads concurrently.

What is concurrent Dictionary?

Definition. ConcurrentDictionary is a generic collection, ConcurrentDictionary was introduced in . NET framework 4.0 as it is available in System. Collections. Concurrent namespace, this generic collection is used in the case of a multi-threaded application.


1 Answers

Though it is not an official document, this MSDN blog post can be helpful. The gist of that article: casting to ICollection and calling its Remove method, just as described in the question, is the way to go.

Here's a snippet from the above blog post, which wraps it into a TryRemove extension methods:

public static bool TryRemove<TKey, TValue>(
    this ConcurrentDictionary<TKey, TValue> dictionary, TKey key, TValue value)
{
    if (dictionary == null)
      throw new ArgumentNullException("dictionary");
    return ((ICollection<KeyValuePair<TKey, TValue>>)dictionary).Remove(
        new KeyValuePair<TKey, TValue>(key, value));
}
like image 135
alexm Avatar answered Sep 24 '22 03:09

alexm