If w3wp.exe is the process responsible to cater all the incoming requests for the web application (correct me if i am wrong), and if I have a class let's call it Customer like this
public class Customer
{
public string FirstName{get;set;}
public string LastName{get;set;}
}
So now when this class is accessed in the code like this
var customer = new Customer();
An instance is created in the heap and all the threads running in the w3wp.exe have access to the properties FirstName and LastName of the customer object.
开发者_开发问答So, is FirstName and LastName properties not thread safe in this case ? Is it always wise to use private properties that belongs only to a specific instance and is thread safe?
While the compiler creates a backing field for automatic properties, it doesn't create any synchronization on it.
As for thread safety - it depends what you are doing with an object of this class in your threads. If you have a different object per thread, no problem.
Things are a bit different if you are accessing the same object (i.e. a shared resource) from many threads:
If all you are doing is reading a value and not setting it from multiple threads, there is no problem.
A problem may occur if you are modifying the object from multiple threads. This will require synchronization, something you can add in your threaded code.
Usually it is ok as Oded points out. But if you are worried about threading issues don't let the compiler genearate the backing field for you but write it out yourself prefixing it volatile. The app will pay a performance penalty for that.
If you are really worried about threading you can manually interlock them using this method on assignment.
Best practice I've seen is using simple lock
around code + volatile
prefix for variables in my code which apperas in multithreading context. Never had a problem using this technique.
You don't have the concept very clear about how the worker process works. Essentially for each thread a new instance of your class is created. So effectively, your class does not need to be thread safe and in fact is not thread safe.
Just to be clear, you understanding of private/public fields and thread safety is not at all correct (I don't believe). So in this context you should not think of them as such.
An instance is created in the heap and all the threads running in the w3wp.exe have access to the properties FirstName and LastName of the customer object.
No, not all the threads. Each threads will get it's own instance. I'm assuming here that you've creating an instance of your customer in a page or controller? Think of it this way, each request that your application receives is a thread and because for each request a new instance of a page (or controller) is created, any objects you create in a page or controller are also specific to that thread and so for the most part you're safe. Of course if you're doing something that you've not mentioned or you're not creating instances of your customer object in a page or controller then all kinds of other questions/scenarios will need to be answered before you have your answer.
I've written a post on my blog that explains how all this works. The purpose of the post is different but I believe what you'll learn there will help you get a better grasp of how IIS and Worker process work and how they interact with your ASP.NET application.
ASP.NET Performance-Instantiating Business Layers
精彩评论