ConcurrentDictionary<TKey,TValue> is a thread-safe alternative to Dictionary<TKey,TValue> for key-value access from multiple threads. Its concurrent collections use fine-grained locking and lockless techniques, while its interface combines operations that commonly belong together to avoid race conditions. section 13.2 section 13.5
Use conditional operations instead of separating a check from its update:
- TryGetValue reads an existing value and reports whether the key was found. section 13.2.1
- TryAdd inserts a value and returns false when another thread has already added the key, treating that race as a normal result rather than an exception. section 13.2.1
- GetOrAdd combines lookup and creation for an in-process cache: var item = dictionary.GetOrAdd(itemId, CreateAndInitializeItem). section 13.2.1
- TryUpdate supports compare-and-update logic; retry until it succeeds when incrementing a value concurrently. section 13.2.1
Concurrent increment
private ConcurrentDictionary<string, int> _dictionary = new(); public void Increment(string key) { while (true) { int prevValue = _dictionary[key]; if (_dictionary.TryUpdate(key, prevValue + 1, prevValue)) break; } } The expected previous value acts as a condition: a competing update causes TryUpdate to fail, so the loop rereads and retries. section 13.2.1
Listing 13.8 Thread-safe cache with ConcurrentDictionary.GetOrAdd
var item = dictionary.GetOrAdd(itemId,CreateAndInitializeItem);
Browse the compact cache implementation using GetOrAdd. section 13.2.1
