When will ConcurrentDictionary TryRemove return false

后端 未结 3 1673
情深已故
情深已故 2020-12-29 00:57

Will it only return false if the dictionary does not contain a value for the given key or will it also return false due to thread race conditions, like another thread adds/u

3条回答
  •  暖寄归人
    2020-12-29 01:22

    One other point to make:

    // This might fail if another thread is adding with key value of 1.
    cd.TryAdd(1, "one"); 
    

    This comment is incorrect and possibly suffers from the same misconception about what it means to 'try'. It's not about a concurrent attempt to add, it's whether a value has already been added with key 1.

    Consider a standard Dictionary. The equivalent code would be:

    if (!d.Contains(1))
        d.Add(1, "one");
    

    This requires two operations. There's no way to design such an API to be threadsafe, as cd might have a value with key 1 added between the call to Contains and Add, which would then result in Add throwing.

    The concurrent collections have APIs that logically bundle these test-and-do pairs into single atomic operations, behind a single API.

提交回复
热议问题