C# parallel programming modifying xDocument
I never tried parallel programming before in c#. So, before I jumped in, I wish I can get a fast answer to know if it worth delving into it, or not. I have C# WCF web services applications with .NET 4.0. (it is possible to upgrade to 4.5 if parallel programming works)
All services are REST Services. There is one service in particular that is taking sometimes very long time. The service is processing and modifying an xml document. the service accept an xml string as input, and give back the modified xml file.
The service does processing the xml in different locations, and different elements. So, I created classes that inherits from an interface called IDocumentProcessor, and I have a list of those
The code briefly looks like this
interface IDocumentProcessor {
void Process(XDocument doc);
}
public class DateProcessor : IDocumentProcessor
{
public void Process(XDocument doc) {....};
}
public class CountryProcessor : IDocumentProcessor
{
public void Process(XDocument doc) {....};
}
public class AddressProcessor : IDocumentProcessor
{
public void Process(XDocument doc) {....};
}
public class AuthorProcessor : IDocumentProcessor
{
public void Process(XDocument doc) {....};
}
....
Public class DocumentProcessorService
{
public class ProcessDocument(string xmlFileAsString)
{
var processorList = new List<IDocumentProcessor>{
new DateProcessor();
new CountryProcessor();
new AddressProcessor();
new AuthorProcessor();
}
var xDocument = XDocument.Parse(xmlFileAsString);
processorList.forEach(x => x.Process(xDocument));
}
}
so my fast question, and before I delve into making this parallel: Can parallel computing modify the same xDocument object (in different locations)
and can this code be translated to do parallel computing with .net 4.0?
From XElement documentation:
Thread Safety Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
It basically means that you cant modify an XDocument in parallel.
Another issue is that its not practical to fork on a type of operation on the same data.
This way its not scalable.
There might not be enough operation types to a fork for every CPU core and one type might finish much faster then the other. Also the contention is high.
If your document contains a collection of high level elements of a similar scheme, you could process their copies in parallel and then replace the old ones with the new ones.
The reassembly operation must be done in one thread and should not be too expensive if you choose the correct level of granularity.
You basically need a copy constructor for each XElement which is to be processed.
var newElements = collectionElement.Elements().Select(el=>
Process(new XElement(el))).AsParallel();
var newCollection = new XElement("items", newElements);
Can multiple thread modify instance of XDocument
at the same time - yes, there is nothing explicitly stopping thread to make changes (unlike for example UI operations in WinForms/WPF).
But since XDocument
type is not thread safe class the results are completely unpredictable.
Correct implementation should prevent parallel access to the same XDocument
(i..e using lock
around access operations), as long as access is serialized you can change it from any thread.
上一篇: 对列表中的所有项目不执行操作的C#Parallel.Foreach
下一篇: C#并行编程修改xDocument