XElement的add操作线程安全吗?

发布于 2024-10-03 06:41:36 字数 68 浏览 0 评论 0原文

通过 ADD 方法以并行方式(如并行 foreach)将子元素添加到 XElement 是否安全?

谢谢!

Is it safe to Add child elements to an XElement via its ADD method in a parallel way (as in a parallel foreach)?

Thanks!

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

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

发布评论

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

评论(2

千と千尋 2024-10-10 06:41:36

来自文档

任何公共静态(在视觉中共享
基本)该类型的成员是线程
安全的。任何实例成员都不是
保证线程安全。

换句话说,不,add 方法不是线程安全的。

From the documentation:

Any public static (Shared in Visual
Basic) members of this type are thread
safe. Any instance members are not
guaranteed to be thread safe.

In other words, no, the add method is not thread safe.

若无相欠,怎会相见 2024-10-10 06:41:36

最近(痛苦的)经历让我认为线程非常非常不安全。

我分析了几个转储文件,其中分别包含 740 万个和 880 万个额外不需要的 XElement 实例。据我所知,它们是在两个线程池线程同时调用一些涉及操作 XElement 子节点(包括在某些情况下调用 Add())的看似无害的代码时创建的。

这并不奇怪 - XElement 和关联的类是使用类似链接列表的结构实现的,如果您打乱链接引用,可能会出现各种奇怪的结果 - 循环、未附加的片段等。

Recent (painful) experience leads me to think that it is very, VERY thread un-safe.

I analyzed a couple of dump files that contained 7.4 million and 8.8 million extra unwanted XElement instances respectively. As best I can determine, they got created when some innocuous-looking code involving manipulating the sub-nodes of an XElement (including calling Add() in some cases) got called from two thread pool threads at the same time.

This isn't too surprising - XElement and the associated classes are implemented using linked-list-like structures, and if you scramble the link references a variety of weird results can ensue - loops, unattached fragments, etc.

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