Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Friday, May 18, 2007 3:01 PM
I was having a discussion with a friend about my usage of static classes and he stated that static classes are not thread safe. I didn't have the concrete knowledge to refute his claim but I have to think that static classes are thread safe by nature. My cursory research also supports my thinking. In addition, if applied inside of time/resource sensitive applications, I would think the use of static classes would be faster and less resource intensive since an instance of a class AND a reference to that instance do not need to be created.
Since static classes are not instantiated, does one have to worry about threading issues if two different threads are calling the same static method of a static class?
It should be noted that I only use static classes when the methods of the class do not rely on any instance members. Rather, just the input parameters and any variables within the scope of the method are used.
If anyone could offer their knowledge it would be much appreciated.
Thanks,
Paul
All replies (5)
Friday, May 18, 2007 4:47 PM âś…Answered | 1 vote
| ShellShock wrote: | ||||
|
No, they are not inherently thread safe (unless they do not call any code but you get that with instance methods too).
Thread safety also includes synchronizing globally accessible resources. A static method may not access any static members of the same class; but it can still have thread safety issues.
For example, if a static method opened a specific file in one thread it would not deny any other thread in the same application from running but each thread could be interfering with the final data being written to disk without some sort of synchronization. Same could occur with COM ports, named kernel objects (mutexes, events, pipes, semaphores, etc.), sockets, etc. Or, if the static method calls a method that may affect application or globally applicable state.
A static method should be scrutinized for thread-safety just as much as instance methods but you don't have to worry about synchronizing instance methods. Lack of access to static members in a static method should not be a indication you don't have to perform any other thread safety checks.
Friday, May 18, 2007 3:11 PM
No, static classes aren't inherently thread safe. Static classes don't suffer from the thread-safety issue of instance-shared data; but all other thread-safety issues still apply.
Friday, May 18, 2007 3:12 PM
For example, a static class may have a static Stream member. If two threads acess that static member without synchronization you'd have very strange errors.
Friday, May 18, 2007 4:26 PM
| pkoanui wrote: | |
|
This is exactly when static classes are thread safe - as long as you do not access any static fields within the class.
Friday, May 18, 2007 4:49 PM
| Peter Ritchie wrote: | |
|
Bad example in the context; but access to static members within a static method is but one thread safety hot point.