Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7041509
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:03:16+00:00 2026-05-28T02:03:16+00:00

The regular Thread Safety section of the MSDN documentation for StringBuilder states that: …any

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T02:03:17+00:00Added an answer on May 28, 2026 at 2:03 am

    Absolutely not; here’s a simple example lifted from 4.0 via reflector:

    [SecuritySafeCritical]
    public StringBuilder Append(char value)
    {
        if (this.m_ChunkLength < this.m_ChunkChars.Length)
        {
            this.m_ChunkChars[this.m_ChunkLength++] = value;
        }
        else
        {
            this.Append(value, 1);
        }
        return this;
    }
    

    The attribute just handles callers, not thread-safety; this is absolutely not thread-safe.

    Update: looking at the source he references, this is clearly not the current .NET 4.0 code-base (comparing a few methods). Perhaps he is talking about a particular .NET version, or maybe XNA – but it is not the case in general. The 4.0 StringBuilder does not have a m_currentThread field, which Gavin’s source material uses; there’s a hint (an unused constant ThreadIDField) that it used to exist, but… no longer.


    If you want a direct disproof – run this on 4.0; it will most likely give the wrong length (I’ve seen a few in the 4k region, a few in the 2k region – it should be exactly 5000), but some other Append methods (Append(char) for example) tend more likely to throw exceptions, depending on timing:

    var gate = new ManualResetEvent(false);
    var allDone = new AutoResetEvent(false);
    int counter = 0;
    var sb = new StringBuilder();
    ThreadStart work = delegate
    {
        // open gate when all 5 threads are running
        if (Interlocked.Increment(ref counter) == 5) gate.Set();
        else gate.WaitOne();
    
        for (int i = 0; i < 1000; i++) sb.Append("a");
    
        if (Interlocked.Decrement(ref counter) == 0) allDone.Set();
    };
    for(int i = 0 ; i < 5 ; i++)
    {
        new Thread(work).Start();
    }
    allDone.WaitOne();
    Console.WriteLine(sb.Length);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have app that uses two threads: one thread for regular application work and
I want to compile only the thread and regular expresession library of boost and
So regular expressions seem to match on the longest possible match. For instance: public
At regular intervals we are receiving CSV files from an external source that we
If a thread is running a function func1 that calls another function func2 inside
I have a regular expression that looks through html content for some keywords that
I have a process I run periodically on a background thread that receives changes
I wrote a regular expression that will replace odd number of slashes by an
I am trying to implement a queue thread that listen to some messages. What
I have a worker thread that is listening to a TCP socket for incoming

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.