Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
I'm testing the ThreadStatic attribute in a simple class.
Here's the code:

C#
public class TestClass
    {
        [ThreadStatic ( )] 
        static int reps = 0;

        [ThreadStatic ( )]
        static int count = 20;

        public void Run ( )
        {
            var t1 = new Task ( ( ) =>
                {
                    for ( int i = 0; i < count; i++ )
                    {
                        Console.WriteLine ( "Thread 1:" + reps++ );
                        Thread.Sleep ( 0 );
                    }
                } );

            var t2 = new Task ( ( ) =>
            {
                for ( int i = 0; i < 20; i++ )
                {
                    Console.WriteLine ( "Thread 2:" + reps++ );
                    Thread.Sleep ( 0 );
                }
            } );



            t1.Start ( );
            t2.Start ( );

            t1.ContinueWith ( ( e ) => 
            { 
                Console.WriteLine ( e.Status ); 
            }, TaskContinuationOptions.OnlyOnRanToCompletion );
            
            t2.ContinueWith ( ( e ) => 
            {
                Console.WriteLine ( e.Status );
            }, TaskContinuationOptions.OnlyOnRanToCompletion );
        }
    }



Here's the output:
Thread 2:0
Thread 2:1
Thread 2:2
Thread 2:3
Thread 2:4
Thread 2:5
Thread 2:6
Thread 2:7
Thread 2:8
Thread 2:9
Thread 2:10
Thread 2:11
Thread 2:12
Thread 2:13
Thread 2:14
Thread 2:15
Thread 2:16
Thread 2:17
Thread 2:18
Thread 2:19
RanToCompletion
RanToCompletion


As you can see only the 2nd Task outputs something. The only difference between the two Tasks is that in the first one I use a static int variable marked with the ThreadStatic attribute to calculate the exit condition of the for loop.

Why the first Task does not output the values?

Thanks
Posted
Comments
Sergey Alexandrovich Kryukov 7-Aug-15 10:40am    
This is a nice educative experiment, but you would easily add more output and dig out what's going on by yourself. Nevertheless, I up-voted your question by 4.
—SA

This is because count is thread-static, one task is using this value, and another one uses hard-coded immediate constant 20 (bad by itself). A thread-static field becomes not static, the attribute ThreadStaticAttribute makes it behave as two or more different static fields per each thread, which are, importantly, are initialized separately in each thread. Its initial value becomes 0, so the loop using it will undergo zero number of iterations. To see it, if would be enough to output count before each loop.

Please see: https://msdn.microsoft.com/en-us/library/system.threadstaticattribute%28v=vs.110%29.aspx[^].

Note that having such field would be pointless, because this is constant, so its value can be used in two thread. To make both tasks' loops work, you would need to declare
C#
const count = 20; // without the attribute
and remove hard-coded 20 from other places.

Note that your experiment is not 100% correct, because tasks are not exactly threads.

—SA
 
Share this answer
 
Find out by myself looking on MSDN.

https://msdn.microsoft.com/en-us/library/system.threadstaticattribute.aspx[^]

Remainder for me: RTFM!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900