从如下的对象读取线程安全的? [英] Is reading from an XmlDocument object thread safe?

查看:94
本文介绍了从如下的对象读取线程安全的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在想,如果我能安全使用的SelectNodes()从多个线程的SelectSingleNode()XmlDocument对象并没有问题阅读。 MSDN说,他们都不能保证线程安全的。如果从多个线程运行的SelectNodes()和的SelectSingleNode()不存在的问题,我可以用适当的锁,以避免任何问题?我已成立了一个WCF服务,需要从数据库的XML抢一大块,并选择了该XML的一些信息。我想缓存XML,以避免击中数据库,以便经常,但我很担心线程安全性和性能。有没有更好的方式去这样做呢?谢谢

I was wondering if i could safely read from an XmlDocument object using SelectNodes() and SelectSingleNode() from multiple threads with no problems. MSDN says that they are not guaranteed to be thread safe. If SelectNodes() and SelectSingleNode() do present problems running from multiple threads, could i use proper locking to avoid any issues? I have a WCF service set up that needs to grab a chunk of xml from a database and select some info out of this xml. I'd like to cache the xml to avoid hitting the database so often, but i'm concerned about thread safety and performance. Is there a better way to go about doing this? Thanks

推荐答案

这里的交易。如果文档中说,实例方法并不保证的广告是线程安全的,那么你最好注意一下。如果你决定使用这个类在多线程情况下没有适当的同步机制,那么你需要为1)注意忽略文档的后果,并2)所有的假设的准备要在类的未来版本无效。这个建议也有效的,似乎只能读取内部状态的方法。

Here's the deal. If the documentation says that instance methods are not guarenteed to be threadsafe then you better take note. And if you do decide to use the class in a multithreaded scenario without the proper synchronization mechanisms then you need to be 1) aware the consequences of ignoring the documentation and 2) prepared for all of your assumptions to be invalidated on future versions of the class. This advice is valid even for methods that seem to only be reading internal state.

你怎么知道的SelectNodes和SelectSingleNodes不修改的内部变量?因为如果他们这样做,然后他们肯定不是线程安全的!现在,我碰巧使用反射来看看里面,我可以看到他们不修改任何内部变量。但是,你怎么知道会不会在未来的版本中改变?

How do you know that SelectNodes and SelectSingleNodes do not modify an internal variable? Because if they do then they are definitely not threadsafe! Now, I happen to use Reflector to look inside and I can see that they do not modify any internal variables. But, how do you know that would not change in a future version?

现在,因为我们知道在现实中的SelectNodes和SelectSingleNodes不修改类的内部状态他们的可能的不顾警告,对多线程操作安全,当且仅当以下条件适用。

Now, since we know in reality that SelectNodes and SelectSingleNodes do not modify the internal state of the class they may be safe for multithreaded operations despite the warning if and only if the following conditions apply.


  • 后, XmlDocument的初始化没有其他方法之外的SelectNodes或的SelectSingleNode被称为有史以来...。因为我还没有检查了XmlDocument类的所有方法我不能说那些修改类的内部状态,哪些没有,因此我会考虑所有,但2种方法我刚才提到一个可能的风险,打破你的无锁的方法来使用类。

  • 创建一个显式或隐记忆障碍XmlDocument的是在一个线程和的SelectNodes或SelectSingleNodes之前调用另一个线程初始化之后。我要指出,内存屏障将最有可能被暗中为您创建为获得多线程环境设置的结果。但是,我能想到的一些细微的场景中这打破了。

我的意见...采取的警告文档字面上并使用适当的同步机制。

这篇关于从如下的对象读取线程安全的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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