Are static method threads safe

I have a static timer class that will be called by ANY webpage to calculate how long each page should be built.

My question is: are static classes safe? In my example, are concurrent users causing problems with my start and stop times? for example, different threads overwriting start and stop values.

public static class Timer { private static DateTime _startTime; private static DateTime _stopTime; /// <summary> /// Gets the amount of time taken in milliseconds /// </summary> /// <returns></returns> public static decimal Duration() { TimeSpan duration = _stopTime - _startTime; return duration.Milliseconds; } public static void Start() { _startTime = DateTime.Now; } public static void Stop() { _stopTime = DateTime.Now; } } 

If this class is non-stationary?

(This class is called from the asp.net homepage.)

+45
c # static
Jul 07 '09 at 6:16
source share
4 answers

Static methods are inherently not thread safe. With their help, they are not processed otherwise than instance methods. The difference is that you usually need to try to make them thread safe. (I cannot think of any static .NET BCL methods that are not thread safe.) Instance methods are often not thread safe because a typical template is to create an object and reuse it from a single thread, and if you need to use it from multiple threads, and coordination includes ensuring the safe use of the facility. In so many cases, this is more appropriate to do in the coordinating code than in the object itself. (Normally, you want entire sequences of operations to be efficiently atomic - something that cannot be done in an object.)

Your Timer class is most definitely not thread safe: two threads can easily tear data off one another, and there is nothing to stop the thread from using "stale" data when calculating the duration.

Instead, use Stopwatch - what is it for? Admittedly, if you want to use one instance of several threads, you need to take the usual steps to ensure security, but you will be in a much better position overall. Admittedly, Stopwatch far from perfect - see this question and comment below for more details - but this is at least what the type is for. (Who knows, this could be fixed for a while ...)

+58
Jul 07 '09 at 6:22
source share
— -

There is a good discussion here that focuses more on the mechanisms and reasons why your example is not thread safe.

To summarize, firstly, your static variables will be shared. If you can make them local variables, even if they are local to the static method, they will still get their own stack stack and thus be thread safe. In addition, if you otherwise protect your static variables (i.e. Locks and / or other multi-threaded programming methods mentioned by others in this thread), you can also make your static class sample thread safe.

Secondly, because your example does not accept external instances of variables that you change or whose state a different thread may affect, your example is also thread safe.

+21
Mar 19 '12 at 15:39
source share

Your timer class is definitely not thread safe. You must create a normal class and create it every time you need to measure time:

 Timer timer = new Timer(); timer.Start(); //... timer.Stop(); decimal duration = timer.Duration(); 

Even better, there is a built-in .NET class that does just that:

 Stopwatch sw = Stopwatch.StartNew(); sw.Stop(); TimeSpan duration = sw.Elapsed; 
+18
Jul 07 '09 at 6:22
source share

Yes, you're right, the static members / accessors in this class will force them to be overwritten by different users.

That is why you have instances and non-static elements.

+4
Jul 07 '09 at 6:20
source share



All Articles