0

Does XElement.Remove() function thread safe?such as in Parallel.Foreach.and I can ensure the nodes in different threads are different.

TommyLike
  • 1,010
  • 11
  • 20

1 Answers1

3

None of LINQ to XML is thread-safe for modifications.

In general, you will almost never find a mutable thread-safe object, unless it has been specifically designed for thread-safety (System.Collections.Concurrent).

The one major exception to this rule (Java's original collection framework) is widely regarded as a mistake, and has been replaced with separate concurrent and non-thread-safe versions.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • so I should store the node in **System.Collections.Concurrent** such as ConcurrentBag and remove then all after the thread operation? – TommyLike Oct 23 '13 at 03:10
  • @TommyLike: It entirely depends on what you're doing. Thread-safety is not simple; you need to figure out what might run in parallel and how they might interfere. See also my blog post, http://blog.slaks.net/2013-07-22/thread-safe-data-structures/ – SLaks Oct 23 '13 at 13:25