Are class-level public properties protected by threads

If w3wp.exe is the process responsible for all incoming requests for the web application (correct me if I am wrong), and if I have a class, call it as Customer

public class Customer { public string FirstName{get;set;} public string LastName{get;set;} } 

So now that this class is accessing code like this

  var customer = new Customer(); 

An instance is created on the heap, and all threads running in w3wp.exe have access to the FirstName and LastName properties of the client object.

So, are the FirstName and LastName properties not thread safe in this case? Is it always wise to use private properties that belong only to a particular instance and are thread safe?

+4
source share
3 answers

While the compiler creates a fallback field for automatic properties, it does not create any synchronization on it.

As for thread safety, it depends on what you do with the object of this class in your threads. If you have another object in the stream, no problem.

Everything is a little different if you access the same object (i.e. a shared resource) from many threads:

If everything you do reads the value and does not set it from multiple threads, there is no problem.

A problem may arise if you modify an object from multiple threads. This will require synchronization, which you can add to your threaded code.

+3
source

This is usually normal, as Oded points out. But if you are worried about threading issues, don't let the compiler generate a support field for you, but write for yourself, its prefix is volatile . The application will pay a penalty for this.

If you are really worried about streaming, you can manually block them using this method when assigning.

The best practice I've seen is to use a simple lock code + + t21 prefix for variables in my code that apperas in a multi-threaded context. There has never been a problem using this technique.

0
source

You do not have a clear idea of ​​how the workflow works. Essentially, a new instance of your class is created for each thread. Thus, your class should not be thread safe and not really thread safe.

Just to be clear, you understand that private / public fields and thread safety are not quite right (I don't believe). Therefore, in this context, you should not think of them as such.

An instance is created on the heap and all threads running in w3wp.exe have access to the FirstName and LastName properties of the client object.

No, not all threads. Each thread will receive its own instance. I assume that you are creating an instance of your client on a page or controller? Think about it this way, each request that your application receives is a stream, and since a new instance of the page (or controller) is created for each request, any objects you create on the page or controller also belong to this stream and therefore for the most part parts you are safe. Of course, if you do something that you did not mention, or you do not create instances of your client object on the page or controller, then you will need answers to all other questions / scripts before you get an answer.

I wrote a blog post explaining how it all works. The purpose of the publication is different, but I believe that what you learn will help you better understand how IIS and Worker work, and how they interact with your ASP.NET application.

ASP.NET Performance-Instantaging Business Layers

-1
source

Source: https://habr.com/ru/post/1338810/


All Articles