嵌套字典对于父 ConcurrentDictionary 线程安全吗?

发布于 2025-01-12 19:08:57 字数 606 浏览 1 评论 0 原文

如果我有一个 ConcurrentDictionary 对象 ConcurrentDictionary>() dict;,则嵌套的 Dictionary 会被锁定正在外部 ConcurrentDictionary 上执行操作吗?

场景:外部ConcurrentDictionary outerDict正在执行

outerDict.Add(42, new Dictionary())< /code>

在一个线程上,并且在另一个线程上(同时),内部 Dictionary 正在执行

outerDict[30].Add("hello", “世界”)

在上述场景中,对于外部 ConcurrentDictionary 和嵌套 Dictionary 的修改是否都应用了并发,或者这两个操作同时执行?

If I have a ConcurrentDictionary object ConcurrentDictionary<int, Dictionary<string, string>>() dict;, is the nested Dictionary locked when operations are being performed on the outer ConcurrentDictionary?

Scenario: an outer ConcurrentDictionary outerDict is performing an

outerDict.Add(42, new Dictionary<string, string>())

on one thread, and, on another thread (simultaneously), an inner Dictionary is performing an

outerDict[30].Add("hello", "world").

Is concurrency applied to the modification of both the outer ConcurrentDictionary and the nested Dictionary in the above scenario, or are both operations performed at the same time?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

时光匆匆的小流年 2025-01-19 19:08:57

当然不是,它们是具有不同访问规则的不同词典。

不过,您的示例很好,因为您正在从不同的线程访问不同的字典。如果您这样做:

outerDict[30]["a"] = "b"; // in thread 1
outerDict[30]["g"] = "h"; // in thread 2

您很快就会遇到问题。

Of course not, they're different dictionaries with different access rules.

Your example however is fine, because you're accessing different dictionaries from different threads. If you were to do this instead:

outerDict[30]["a"] = "b"; // in thread 1
outerDict[30]["g"] = "h"; // in thread 2

You'd quickly run into issues.

一生独一 2025-01-19 19:08:57

这里有4个操作:

  • 创建一个新对象。这样做不会影响并发性,因为当时没有其他人引用该对象。
  • 将对象添加到并发字典中。没问题,因为它是并发字典。
  • 访问并发字典中的第 30 个条目。没问题,因为它是一个并发字典,
  • 向第 30 个字典添加新值:有问题,如果另一个线程也引用了第 30 个字典。

There are 4 operations here:

  • creating a new object. Doing that has no concurrency impact, since nobody else has a reference to the object at that time.
  • adding the object to the concurrent dictionary. No problem, because it's a concurrent dictionary.
  • accessing the 30th entry in the concurrent dictionary. No problem, because it's a concurrent dictionary
  • adding a new value to that 30th dictionary: problematic, if another thread has a reference to the 30th dictionary as well.
一个人的旅程 2025-01-19 19:08:57

ConcurrentDictionary 是线程安全的。它的设计使多个线程可以安全地使用该字典中的键。它不能也不会将该线程安全性扩展到存储在字典中的值。

并发字典存储对内部字典的引用。但内部词典并不“知道”任何引用它的内容。据它所知,它只是一个 Dictionary

尽管并发字典具有对内部字典的引用,但它不以任何方式“拥有”或控制该内部字典。

您可以编写以下代码:

var innerDictionary = new Dictionary<string, string>();
outerDict.Add(42, innerDictionary);

在这种情况下,innerDictionary 仍然保留对该字典的引用。它可以将其作为参数传递给另一个方法。并发字典可能会超出范围并被垃圾收集,而其他一些对象则维护对内部字典的引用。

要点是,除了持有对象引用的并发字典之外,它不会以其他方式控制该对象的行为。

A ConcurrentDictionary<TKey, TValue> is thread safe. It's designed so that multiple threads can safely work with the keys in that dictionary. It cannot and does not extend that thread safety to the values stored in the dictionary.

The concurrent dictionary stores a reference to the inner dictionary. But the inner dictionary doesn't "know" about anything that references it. As far as it knows it's just a Dictionary<string, string>.

Although the concurrent dictionary has a reference to the inner dictionary, it doesn't in any way "own" or control that inner dictionary.

You could write this code:

var innerDictionary = new Dictionary<string, string>();
outerDict.Add(42, innerDictionary);

In this case innerDictionary still maintains a reference to that dictionary. It can pass it as an argument to another method. The concurrent dictionary could go out of scope and get garbage collected while some other object maintains a reference to the inner dictionary.

The point is that other than the concurrent dictionary holding a reference to an object, it doesn't otherwise control the behavior of that object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文