.NET - 字典锁定与ConcurrentDictionary [英] .NET - Dictionary locking vs. ConcurrentDictionary

查看:155
本文介绍了.NET - 字典锁定与ConcurrentDictionary的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 ConcurrentDictionary 类型中找不到足够的信息,所以我想我会在这里问一下。

I couldn't find enough information on ConcurrentDictionary types, so I thought I'd ask about it here.

目前,我使用字典保存所有由多个线程(来自线程池,因此没有精确的线程数)不断访问的用户,同步访问。

Currently, I use a Dictionary to hold all users that is accessed constantly by multiple threads (from a thread pool, so no exact amount of threads), and it has synchronized access.

我最近发现在.NET 4.0中有一组线程安全的集合,它似乎是非常愉快的。我想知道,什么是更高效和更容易管理选项,因为我有一个选择之间有正常字典与同步访问,或有一个 ConcurrentDictionary 已经是线程安全的。

I recently found out that there was a set of thread-safe collections in .NET 4.0, and it seems to be very pleasing. I was wondering, what would be the 'more efficient and easier to manage' option, as i have the option between having a normal Dictionary with synchronized access, or have a ConcurrentDictionary which is already thread-safe.

参考.NET 4.0的 ConcurrentDictionary

推荐答案

线程安全集合与非线程安全集合可以以不同的方式查看。

A thread-safe collection vs. a non-threadsafe-collection can be looked upon in a different way.

考虑一个没有店员的商店,除了结帐。如果人们不负责任地行事,你有很多问题。例如,假设客户从金字塔罐头取罐,而店员正在建造金字塔,所有的地方都会松动。或者,如果两个客户同时到达同一个项目,谁赢了?会有战斗吗?这是一个非线程安全集合。有很多方法可以避免问题,但是它们都需要某种锁定,或者以某种方式显式访问。

Consider a store with no clerk, except at checkout. You have a ton of problems if people don't act responsibly. For instance, let's say a customer takes a can from a pyramid-can while a clerk is currently building the pyramid, all hell would break loose. Or, what if two customers reaches for the same item at the same time, who wins? Will there be a fight? This is a non-threadsafe-collection. There's plenty of ways to avoid problems, but they all require some kind of locking, or rather explicit access in some way or another.

另一方面,考虑一个商店与一个办事员在一张桌子,你只能通过他购物。你进来,问他一件物品,他把它带回你,你出去了。如果你需要多个项目,你只能在每次往返只能提取尽可能多的项目,你可以记住,但你需要小心,以避免徘徊的职员,这将愤怒在你身后的其他客户。

On the other hand, consider a store with a clerk at a desk, and you can only shop through him. You get in line, and ask him for an item, he brings it back to you, and you go out of the line. If you need multiple items, you can only pick up as many items on each roundtrip as you can remember, but you need to be careful to avoid hogging the clerk, this will anger the other customers in line behind you.

现在考虑这个。在有一个店员的商店,如果你得到所有的方式到线的前面,并问店员你有卫生纸,他说是,然后你去好吧,我'当我知道我需要多少钱时,我会得到你的回信,那么当你回到线路的前面时,商店当然可以卖完了。线程安全集合不能防止此情况。

Now consider this. In the store with one clerk, what if you get all the way to the front of the line, and ask the clerk "Do you have any toilet paper", and he says "Yes", and then you go "Ok, I'll get back to you when I know how much I need", then by the time you're back at the front of the line, the store can of course be sold out. This scenario is not prevented by a threadsafe collection.

线程安全集合保证其内部数据结构始终有效,即使从多个线程访问。

A threadsafe collection guarantees that its internal data structures are valid at all times, even if accessed from multiple threads.

非线程安全集合不附带任何此类保证。例如,如果你在一个线程上添加一个二叉树,而另一个线程正在忙于重新平衡树,那么就不能保证该项目将被添加,或者即使该树仍然有效,它可能会超出希望。

A non-threadsafe collection does not come with any such guarantees. For instance, if you add something to a binary tree on one thread, while another thread is busy rebalancing the tree, there's no guarantee the item will be added, or even that the tree is still valid afterwards, it might be corrupt beyond hope.

然而,线程安全集合并不保证线程上的顺序操作都在其内部数据结构的相同快照上工作,这意味着如果您代码如下:

A threadsafe collection does not, however, guarantee that sequential operations on the thread all work on the same "snapshot" of its internal data structure, which means that if you have code like this:

if (tree.Count > 0)
    Debug.WriteLine(tree.First().ToString());

您可能会得到一个NullReferenceException,因为 tree.Count tree.First(),另一个线程已经清除树中剩余的节点,这意味着 First()将返回 null

you might get a NullReferenceException because inbetween tree.Count and tree.First(), another thread has cleared out the remaining nodes in the tree, which means First() will return null.

在这种情况下,您需要查看安全的方式来获得你想要的,也许你需要重写上面的代码,或者你可能需要锁定。

For this scenario, you either need to see if the collection in question has a safe way to get what you want, perhaps you need to rewrite the code above, or you might need to lock.

这篇关于.NET - 字典锁定与ConcurrentDictionary的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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