none
[ThreadStatic] property accessed from public method of the class. RRS feed

  • Question

  • Please look at the below code in the question:

        public sealed class AverageTimerPerformanceCounter : IDisposable
        {

            [ThreadStatic]
            private static Stopwatch stopwatch;

            public void Start()
            {
                if (stopwatch == null)
                {
                    stopwatch = new Stopwatch();
                }

              stopwatch.Restart();
            }

    }

    As you can see the Start method is not static, however it will be updating the static property stopWatch. As the property has also been marked as [ThreadStatic] I assume that in multi-thread scenarios it will persist its value on thread basis (I feel ashamed of so less knowledges on threads :( )

    However As far as I understand, static are not in the scope of the class, it doensnt' matter if the method Start() is class method or I simply change it to static method. So my new class will be as follows:

        public sealed class AverageTimerPerformanceCounter : IDisposable
        {

            [ThreadStatic]
            private static Stopwatch stopwatch;

            public static void Start()
            {
                if (stopwatch == null)
                {
                    stopwatch = new Stopwatch();
                }

              stopwatch.Restart();
            }

    }

    Can somebody please confirm that apart from the change of how I call Start method now (i.e., previously it was called using objects and now will be through class directly), the behavior of the property stopWatch will not change.

    Thanks.

    Monday, October 28, 2013 1:25 PM

Answers

  • Yes. There will not be any change in the behavior. Each thread will get the separate instance of Stopwatch. To ensure, you can execute below code. The code will print "First" and "Second" and then "False" which tells that each thread created their own Stopwatch instance,

    class Program
    {
        static void Main(string[] args)
        {
            Thread t1 = new Thread(AverageTimerPerformanceCounter.Start) { Name = "First" };
            Thread t2 = new Thread(AverageTimerPerformanceCounter.Start) { Name = "Second" };
    
    
            Stopwatch w1, w2;
    
            t1.Start();
            w1 = AverageTimerPerformanceCounter.watchMe;
            Thread.Sleep(500);
            t2.Start();
            Thread.Sleep(500);
            w2 = AverageTimerPerformanceCounter.watchMe;
    
            Console.WriteLine();
            Console.WriteLine(w1 == w2);
            Console.ReadLine();
        }
    }
    
    public sealed class AverageTimerPerformanceCounter
    {
        public static Stopwatch watchMe;
    
        [ThreadStatic]
        public static Stopwatch stopwatch;
    
        public static void Start()
        {
            if (stopwatch == null)
            {
                Console.WriteLine(Thread.CurrentThread.Name);
                stopwatch = new Stopwatch();
                watchMe = stopwatch;
            }
    
            stopwatch.Restart();
        }
    }

    I hope this helps.


    Please mark this post as answer if it solved your problem. Happy Programming!

    • Marked as answer by Mukesh.Joshi Monday, October 28, 2013 4:49 PM
    Monday, October 28, 2013 1:49 PM