Thread safety when using XML API in VB.NET -
i have question thread safety using xml in vb.net.
i have application manages xmldocument
object user creates new items/makes changes existing items. know need synchronize calls xmldocument.createelement(...)
. question is, can proceed build returned element without synchronization, synchronize again when appending element xmldocument
?
this think can do, need make sure thread-safe think is:
' "doc" object exists xmldocument synclock doc dim newsub xmlelement = doc.createelement("submission") end synclock ' use "newsub" here without synchronization synclock doc doc.item("submissions").appendchild(newsub) end synclock
when adding children of "newsub" synchronize when creating each element.
as followup question, better off synchronizing entire building of "newsub" object? reason think doing above better performance, not means expert in whether making meaningful impact on performance, or complicating things.
in general, when using class derived xmlnode, need synchronization, it's documentation explicitly states:
any public static (shared in visual basic) members of type thread safe. instance members not guaranteed thread safe.
this means you'll need synchronization when appending children, you've shown.
as followup question, better off synchronizing entire building of "newsub" object? reason think doing above better performance, not means expert in whether making meaningful impact on performance, or complicating things.
it depends - if you're going doing may cause usable multiple threads, may need synchronize it.
in above code, should safe work newsub
outside of synchronization, since it's not part of actual document tree until append child. reduce amount of time doc
locked, could reduce contention if doc
being used multiple threads.
Comments
Post a Comment