如何对ConcurrentDictionary实现条件的TryRemove? [英] How to implement TryRemove conditional to ConcurrentDictionary?

查看:209
本文介绍了如何对ConcurrentDictionary实现条件的TryRemove?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我需要 Dictionary ,我可以从多个线程中进行更新,显然,该工作的候选人是内置 ConcurrentDictionary 。不幸的是,我最终没有使用它,而是使用了受 lock 保护的普通 Dictionary TryRemove 不提供允许有条件删除元素的重载。可用的 TryRemove 方法将删除并返回已删除的元素,但在我的情况下,只有在相同工作流程中早先插入该元素时,才必须删除该元素。从不同的工作流程中删除元素(即使是一微秒的时间)也可能会带来不良后果,而我宁愿不必解决。所以我的问题是:是否可以使用线程安全的条件 TryRemove 扩展方法来修改现有的 ConcurrentDictionary 类?

Recently I had a need for a Dictionary that I could update from multiple threads, and the obvious candidate for this job was the build-in ConcurrentDictionary. Unfortunately I ended up not using it, and using instead a normal Dictionary protected with a lock, because of a fatal limitation: the TryRemove does not offer an overload that allows the conditional removal of an element. The available TryRemove method removes and returns the removed element, but in my case it was mandatory to remove the element only if it was inserted earlier by the same workflow. Removing an element from a different workflow (even for a fraction of a μsec) could have undesirable consequences that I would prefer not to have to resolve. So my question is: Is it possible to amend the existing ConcurrentDictionaryclass with a thread-safe conditional TryRemove extension method?

作为参考,这是我的用例。使用 <<安全地填充字典code> AddOrUpdate 方法:

For reference here is my use case. The dictionary is populated safely using the AddOrUpdate method:

var dict = new ConcurrentDictionary<string, CancellationTokenSource>();
var cts = dict.AddOrUpdate("Key1", key => new CancellationTokenSource(),
    (key, existingValue) =>
{
    existingValue.Cancel();
    return new CancellationTokenSource();
});

稍后我想通过调用以下不存在的方法来删除我先前插入的值:

Later I would like to remove the value I inserted earlier by calling the non-existent method below:

var removed = dict.TryRemove("Key1", (key, existingValue) =>
{
    return existingValue == cts;
});

这是所需方法的签名:

public static bool TryRemove<TKey, TValue>(
    this ConcurrentDictionary<TKey, TValue> source, TKey key,
    Func<TKey, TValue, bool> predicate)
{
    // Is it possible?
}


推荐答案

我认为您需要实施自己的词典,从而以某种方式记住哪个 Task 添加了哪个元素。

I think that you need to implement your own Dictionary that somehow remembers which Task added which element.

但是我建议删除导致此要求的所有细节,因为在我看来,只有通过其创建工作流程才能删除元素,这听起来有些可疑。

But I would recommend to remove what ever detail you have that leaded to this requirement since it sounds somewhat fishy to me that elements can only be removed by its creating workflow.

workflow 1 插入元素 elm1 并在工作时没有将其删除,该怎么办?因此,元素 elm1 保留在词典中,并且自其创建工作流结束以来​​没有人可以删除它。

I say that because what if workflow 1 inserted the element elm1 and didn't removed it while it was working. So the element elm1 remains in the dictionary and nobody can remove it since its creating workflow has ended.

这篇关于如何对ConcurrentDictionary实现条件的TryRemove?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆