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 8067737
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T12:25:24+00:00 2026-06-05T12:25:24+00:00

I was reading through the tips and tricks post and I thought I’d try

  • 0

I was reading through the tips and tricks post and I thought I’d try out some of the C# stuff that I’d never done before. Therefore, the following code serves no actual purpose, but is just a ‘test function’ to see what happens.

Anyway, I have two static private fields:

private static volatile string staticVolatileTestString = "";
[ThreadStatic]
private static int threadInt = 0;

As you can see, I’m testing ThreadStaticAttribute and the volatile keyword.

Anyway, I have a test method that looks like this:

private static string TestThreadStatic() {
    // Firstly I'm creating 10 threads (DEFAULT_TEST_SIZE is 10) and starting them all with an anonymous method
    List<Thread> startedThreads = new List<Thread>();
    for (int i = 0; i < DEFAULT_TEST_SIZE; ++i) {
        Thread t = new Thread(delegate(object o) {
            // The anon method sets a newValue for threadInt and prints the new value to the volatile test string, then waits between 1 and 10 seconds, then prints the value for threadInt to the volatile test string again to confirm that no other thread has changed it
            int newVal = randomNumberGenerator.Next(10, 100);
            staticVolatileTestString += Environment.NewLine + "\tthread " + ((int) o) + " setting threadInt to " + newVal;
            threadInt = newVal;
            Thread.Sleep(randomNumberGenerator.Next(1000, 10000));
            staticVolatileTestString += Environment.NewLine + "\tthread " + ((int) o) + " finished: " + threadInt;
        });
        t.Start(i);
        startedThreads.Add(t);
    }

    foreach (Thread th in startedThreads) th.Join();

    return staticVolatileTestString;
}

What I’d expect to see returned from this function is an output like this:

thread 0 setting threadInt to 88
thread 1 setting threadInt to 97
thread 2 setting threadInt to 11
thread 3 setting threadInt to 84
thread 4 setting threadInt to 67
thread 5 setting threadInt to 46
thread 6 setting threadInt to 94
thread 7 setting threadInt to 60
thread 8 setting threadInt to 11
thread 9 setting threadInt to 81
thread 5 finished: 46
thread 2 finished: 11
thread 4 finished: 67
thread 3 finished: 84
thread 9 finished: 81
thread 6 finished: 94
thread 7 finished: 60
thread 1 finished: 97
thread 8 finished: 11
thread 0 finished: 88

However, what I’m getting is this:

thread 0 setting threadInt to 88
thread 4 setting threadInt to 67
thread 6 setting threadInt to 94
thread 7 setting threadInt to 60
thread 8 setting threadInt to 11
thread 9 setting threadInt to 81
thread 5 finished: 46
thread 2 finished: 11
thread 4 finished: 67
thread 3 finished: 84
thread 9 finished: 81
thread 6 finished: 94
thread 7 finished: 60
thread 1 finished: 97
thread 8 finished: 11
thread 0 finished: 88

The second ‘half’ the output is as expected (which I suppose means that the ThreadStatic field is working like I thought), but it seems like a few of the initial outputs have been ‘skipped’ from the first ‘half’.

Additionally, the threads in the first ‘half’ are out of order, but I understand that a thread does not immediately run as soon as you call Start(); but instead the internal OS controls will be starting the threads as it sees fit.
EDIT: No they’re not, actually, I just thought they were because my brain misses the consecutive numbers


So, my question is: What’s going wrong to cause me to lose a few lines in the first ‘half’ of the output? For example, where is the line ‘thread 3 setting threadInt to 84‘?

  • 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-06-05T12:25:26+00:00Added an answer on June 5, 2026 at 12:25 pm

    Threads are executing at the same time. What conceptually happens is this:

    staticVolatileTestString += Environment.NewLine + "\tthread " + ((int) o) + " setting threadInt to " + newVal;
    
    1. Thread 1 reads staticVolatileTestString
    2. Thread 2 reads staticVolatileTestString
    3. Thread 3 reads staticVolatileTestString
    4. Thread 1 appends the stuff and writes staticVolatileTestString back
    5. Thread 2 appends the stuff and writes staticVolatileTestString back
    6. Thread 3 appends the stuff and writes staticVolatileTestString back

    That causes your lines to be lost. Volatile doesn’t help here; the whole process of concatenating the string is not atomic. You need to use a lock around those operations:

    private static object sync = new object();
    
    lock (sync) {
        staticVolatileTestString += Environment.NewLine + "\tthread " + ((int) o) + " setting threadInt to " + newVal;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Reading through some of the questions here, the general concensus seems to be that
I'm reading through some MSDN example code on Composite controls and finding that there
Reading through the SendAsync , BeginAsync method illustrations of Socket s, I realized that
i've been reading through the linq to xml documentation in msdn and some other
I've been reading through Programming Clojure, and I've been having some trouble understanding Stuarts
Reading through the CKEditor documentation , I see that they have an option to
Whilst reading through the book The well grounded Rubyist, I came across some strange
Reading through some great presentations on low latency computing. They had a reference to
After many hours spent on reading documentations and searching for some tips on the
Reading through the javase api docs, I noticed that pretty much all of the

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.