Welcome Guest. Sign in or Signup

0 Answers

Is .NET’s StringBuilder thread-safe

Asked by: 28 views , ,
.net

The regular “Thread Safety” section of the MSDN documentation for StringBuilder states that “any instance members are not guaranteed to be thread safe” but this statement feels like it has been copied and pasted for almost every class in the Framework:

http://msdn.microsoft.com/en-us/library/system.text.stringbuilder.aspx

However, these blog posts by Gavin Pugh mention thread-safe behaviours of StringBuilder:

http://www.gavpugh.com/2010/03/23/xnac-stringbuilder-to-string-with-no-garbage/

http://www.gavpugh.com/2010/04/01/xnac-avoiding-garbage-when-working-with-stringbuilder/

Additionally, the source of StringBuilder revealed by Reflector, and the accompanying comments
in the SSCLI source, also suggest many implementation considerations to ensure thread-safety:

http://labs.developerfusion.co.uk/SourceViewer/browse.aspx?assembly=SSCLI&namespace=System.Text&type=StringBuilder

Does anyone have any more insight into whether a StringBuilder instance is safe to share among multiple concurrent threads?

newest questions tagged .net – Stack Overflow

Answer Question