Reputation: 750
I have created a CommonUtils.cs file containing 5 static functions (So that I can just "copy" this .cs for other projects in Visual Studio since I develop different C# applications) along with that I have many source files.
Now, I have compiled my project to a DLL. This dll is hosted via IIS server as an application. Many customers use this dll to perform something, say they generate a report.
I have been informed that "static functions" must not be used generously in this context and they should be applied, a "locking" mechanism since without lock, multiple threads of a single instance of program or multiple instances of a program, both can behave unexpectedly. Is it true?
Upvotes: 11
Views: 3188
Reputation: 53958
I have been informed that "static functions" must not be used generously in this context and they should be applied, a "locking" mechanism since without lock, multiple threads of a single instance of program or multiple instances of a program, both can behave unexpectedly. Is it true?
Let's break it piece by piece. What is a static class?
A static class is basically the same as a non-static class, but there is one difference: a static class cannot be instantiated. In other words, you cannot use the new keyword to create a variable of the class type. Because there is no instance variable, you access the members of a static class by using the class name itself.
How CLR treats a static class?
As is the case with all class types, the type information for a static class is loaded by the .NET Framework common language runtime (CLR) when the program that references the class is loaded. The program cannot specify exactly when the class is loaded. However, it is guaranteed to be loaded and to have its fields initialized and its static constructor called before the class is referenced for the first time in your program. A static constructor is only called one time, and a static class remains in memory for the lifetime of the application domain in which your program resides.
Now why we may need locking?
Basically, locking is needed when we have race conditions. When someone may read data that someone else may alter them in the same time. Two separate threads have access to a shared resource and there isn't any mechanism to prevent this. In order you take an answer to your question, you have at first to answer another question.
Does your static methods access shared resources and there might be any race condition? If that's true, then you need to use locking. Otherwise, it is not needed.
For further information about static classes, please have a look here. While if you need more information about thread synchronization techniques, please have a look here.
Upvotes: 11
Reputation: 8208
It sounds like you have a class library. Here are Microsoft's guidelines for class libraries that need to support multi threading:
Avoid the need for synchronization, if possible. This is especially true for heavily used code. For example, an algorithm might be adjusted to tolerate a race condition rather than eliminate it. Unnecessary synchronization decreases performance and creates the possibility of deadlocks and race conditions.
Make static data (Shared in Visual Basic) thread safe by default.
Do not make instance data thread safe by default. Adding locks to create thread-safe code decreases performance, increases lock contention, and creates the possibility for deadlocks to occur. In common application models, only one thread at a time executes user code, which minimizes the need for thread safety. For this reason, the .NET Framework class libraries are not thread safe by default.
Avoid providing static methods that alter static state. In common server scenarios, static state is shared across requests, which means multiple threads can execute that code at the same time. This opens up the possibility of threading bugs. Consider using a design pattern that encapsulates data into instances that are not shared across requests. Furthermore, if static data are synchronized, calls between static methods that alter state can result in deadlocks or redundant synchronization, adversely affecting performance.
Copied from https://msdn.microsoft.com/en-us/library/1c9txz50(v=vs.110).aspx
Upvotes: 0
Reputation: 1411
Explanation for "LOCK" From MSDN says:
The lock keyword marks a statement block as a critical section by obtaining the mutual-exclusion lock for a given object, executing a statement, and then releasing the lock.
The lock keyword ensures that one thread does not enter a critical section of code while another thread is in the critical section. If another thread tries to enter a locked code, it will wait, block, until the object is released
References:
https://msdn.microsoft.com/en-us/library/c5kehkcz.aspx
Its better to use LOCK in Multithreading than creating Static functions everytime.
Upvotes: -1
Reputation: 171178
Functions are immutable so you don't need to synchronize when calling a function. Function parameters are mutable but each invocation has it's own local copy. No need to synchronize either.
Synchronization is required when multiple thread work on the same data and there is at least one writer. This is about any variable that is shared between threads. Care is required for static variables and any instance variable reachable by a static variables.
Upvotes: 9