How To Make ConcurrentDictionary(Of T, T) As Fun As Dictionary(Of T, T)!

Seekers,

I often need a dictionary to hold stuff, but the annoying thing about Dictionary is that if you try to update an existing key, it unceremoniously barfs.

So doing lots of multi-threaded apps these days, I’m using the Concurrent Collections, among them, ConcurrentDictionary.

But there is a little surprise… Where adding to a Dictionary is really simple:

 

…it is not ready for prime-time with multi-threads:

A Dictionary(Of TKey, TValue) can support multiple readers concurrently, as long as the collection is not modified. Even so, enumerating through a collection is intrinsically not a thread-safe procedure. In the rare case where an enumeration contends with write accesses, the collection must be locked during the entire enumeration. To allow the collection to be accessed by multiple threads for reading and writing, you must implement your own synchronization.

For thread-safe alternatives, see the ConcurrentDictionary(Of TKey, TValue) class or ImmutableDictionary class.

No surprise there. So what about the Add() in ConcurrentDictionary?

Description
AddOrUpdate(TKey, Func(Of TKey, TValue), Func(Of TKey, TValue, TValue)) Uses the specified functions to add a key/value pair to the ConcurrentDictionary(Of TKey, TValue) if the key does not already exist, or to update a key/value pair in the ConcurrentDictionary(Of TKey, TValue) if the key already exists.
AddOrUpdate(TKey, TValue, Func(Of TKey, TValue, TValue)) Adds a key/value pair to the ConcurrentDictionary(Of TKey, TValue) if the key does not already exist, or updates a key/value pair in the ConcurrentDictionary(Of TKey, TValue) by using the specified function if the key already exists.

There is no Add()?!

Why not?

…or if this is easier to read:

ConcurrentDictionary(Of TKey, TValue).AddOrUpdate

Method (TKey, TValue, Func(Of TKey, TValue, TValue))

This looks hard, and all I wanted to do was add stuff. And hey, if I already have a key in there the same as what I want to add, let’s just call it an update, fair? I mean, having to check for a duplicate key (or somehow keep track?), then deal with the 2 values every blessed time is a huge waste of time…

In most cases, you just want to add a new key, or update the value of an existing key… If this is your situation (and I bet it is), then this article is for YOU!

Beating my head against the wall a few minutes ago, I stumbled upon some gold on StackOverflow by the CEO of Slicify, Steve. His approach to this dilemma is nothing short of inspiring, and I am really confused as to why M$ failed to even point out that what he did was possible.

His approach (in C#) was to simply override the default behavior of .AddOrUpdate like so:

C#:

VB:

So what does this mean to us? Well, it means that this:

Turns into this:

I’m just now learning about ExtensionMethods, as another good one I saw recently dealt with LINQ queries not natively coming with a .ToDataTable Fluent option…

Throw this somewhere into your project, and you will!

Software is Poetry…

pat
:)

Leave a Reply

Your email address will not be published. Required fields are marked *