简体   繁体   English

如果多次调用GetOrAdd()ValueFactory,那么对其他返回值会如何处理?

[英]What happens to the additional return value if the GetOrAdd() ValueFactory is called multiple times?

In a C# Concurrent Dictionary, specifically the signature . 在C#并发字典中,特别是签名。 . .

public TValue GetOrAdd(TKey key, Func<TKey, TValue> valueFactory)

. . . I know it is possible for valueFactory to be called multiple times. 我知道valueFactory可能被多次调用。 What happens to the multiple return values though? 但是,多个返回值会怎样? Is it guaranteed that only one result will ever be mapped in the dictionary, or is there any possibility that there is a window where other threads can see different values in the dictionary? 是否保证仅在字典中映射一个结果,或者是否存在一个窗口,其他线程可以在字典中看到不同的值?

Looking in reflector the valueFactory is only executed because of an internal method that accepts a value and not a delegate. 在反射器中查看valueFactory仅是由于内部方法接受值而不是委托而执行的。 After that, as I undestand it, it's as if you've called the reguler overload that accepts a value and not a delegate. 在那之后,由于我对此一无所知,就好像您已经调用了接受值而不是委托的reguler重载一样。

so basically, the value from the delegate is ignored, and not mapped to the dictionary. 因此,基本上,该委托的值将被忽略,并且不会映射到字典。 The value in the dictionary is always the same, and as such threads always see the same value (because, using GetOrAdd only one value is ever mapped). 字典中的值始终是相同的,并且这样的线程总是看到相同的值(因为使用GetOrAdd仅映射了一个值)。

Obviously if you change the value using AddOrUpdate values will get updated. 显然,如果使用AddOrUpdate更改值,则值将得到更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM