I read the post tips and tricks , and I thought I would try some of the C # stuff that I had never done before. Therefore, the following code does not serve the actual purpose, but merely is 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 am testing ThreadStaticAttribute and the volatile keyword.
Anyway, I have a validation method that looks like this:
private static string TestThreadStatic() {
What I expect to see from this function is as follows:
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, I get the following:
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 is output as expected (which, I suppose, means the ThreadStatic field works as I thought), but it looks like some of the initial outputs were skipped from the first half.
In addition, the threads in the first half are out of order, but I understand that the thread does not start right away, as soon as you call Start (); but instead, internal OS controls will trigger threads as they see fit. EDIT: No, they are not, in fact, I just thought it was because my brain skips consecutive numbers
So my question is: what's wrong with making me lose a few lines in the first half? For example, where does the string 'thread 3 set threadInt to 84'?