"XmlDocument 的任何公共静态成员都是线程安全的.不保证任何实例成员都是线程安全的": 对,但是 [英] "Any public static members of XmlDocument are thread safe. Any instance members are not guaranteed to be thread safe" : yes, but
问题描述
我在中看到XmlDocument
类文档 在 MSDN 上
I see in the XmlDocument
class documentation on MSDN that
此类型的任何公共静态(在 Visual Basic 中为 Shared)成员都是线程安全的.不保证任何实例成员都是线程安全的.
Any public static ( Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
XmlNodeList
类也是如此.
我在以下上下文中使用这些类.在 Parallel.Foreach
里面我做:
I am using those classes in the following context. Inside a Parallel.Foreach
I do :
X MyX = new X();
string XMLstring = MyX.GetXML(ID, true);
XmlDocument doc = new XmlDocument();
doc.LoadXml(XMLstring);
XmlNodeList nodeList = doc.SelectNodes("blah/secondblah");
X
是在 IT 提供给我的库中定义的,ID
是 int
(我大致在其上循环).
where X
is defined in a library the IT's are providing to me and where ID
is an int
(roughly on which I loop).
这个已经在非并行上下文中彻底测试过,GetXML
产生的string
确实是正确的,对应的XmlDocument
也是如此,并通过XmlNodeList
解析"它提供了预期的结果.
This has been tested thoroughly in a non parallel context, the string
s produced by GetXML
are indeed correct, the corresponding XmlDocument
as well, and "parsing" it via XmlNodeList
provides the expected results.
现在,在这个并行上下文中,并假设 X
和 GetXML
确实是线程安全的,我 new
和 每个循环中的 code>XmlDocument
是否确保线程安全?我的意思是,我怎么知道 string
成员(首先有这样的字符串?因为我在文档中没有看到任何 string
属性)XmlDocument
接收到 LoadXml
是 static
还是不是?
Now, it this parallel context and assuming that X
and GetXML
are indeed thread-safe, does the fact that I new
an XmlDocument
in every loop ensure thread-safety or not ? I mean, how can I know that the string
member (first of all is there such a string ? as I don't see any string
property in the document) of XmlDocument
receiving the LoadXml
is static
or not ?
我想我并不真正理解我上面引用的 MSDN 文档......
I suppose I don't really understand the bit of MSDN documentation I am quoting above ...
推荐答案
文档意味着任何静态方法(看起来像 XmlDocument.MethodCall
是线程安全的.那不是与您相关 - 您不会调用其中任何一个.其他方法(例如针对 doc
)不是静态的 - 因此不能保证它们是线程安全的.
The documentation means that any methods that are static (which would look like XmlDocument.MethodCall
are thread-safe. That isn't relevant to you - you aren't calling any of those. Other methods (e.g. against doc
) are not static - so they are not guaranteed to be thread-safe.
只要 doc
(和 nodeList
和其他非线程安全"变量)仅在单线程.
Your code will be 100% fine, as long as doc
(and nodeList
and other 'non thread-safe' variables) are used solely within the context of a single thread.
因此,如果您在 Parallel.ForEach 之前填充 doc
,然后在 Parallel.ForEach 中使用 doc
inside- 那行不通.
So if you populated doc
before the Parallel.ForEach and then used doc
inside the Parallel.ForEach - that won't work.
但是如果你填充和使用 doc
inside Parallel.ForEach 你会没事的(因为每个线程都会得到它自己的文档——因此线程安全获胜不是问题).
But if you populate and used doc
inside the Parallel.ForEach you will be fine (since each thread will get its 'own doc'- thus thread-safety won't be an issue).
为了 100% 确定,您需要发布整个方法(包括 Parallel.ForEach 调用)以供我们查看.
To be 100% sure, you'd need to post the entire method (including the Parallel.ForEach call) for us to have a look at.
这篇关于"XmlDocument 的任何公共静态成员都是线程安全的.不保证任何实例成员都是线程安全的": 对,但是的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!