Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Thread safety in ASP.Net MVC

I suspect this applies to general ASP.Net too but I am not sure.

If I have an action method on a Controller, say MyController.DoSomethingExciting and three clients hit it at "the same time", is it intrinsically thread safe, or do I need to do something to ensure that the three concurrent calls don't interact with each other?

like image 440
Colin Desmond Avatar asked Jul 09 '09 13:07

Colin Desmond


People also ask

How do you ensure thread safety?

Using Atomic Variable Using an atomic variable is another way to achieve thread-safety in java. When variables are shared by multiple threads, the atomic variable ensures that threads don't crash into each other.

Is ASP net thread-safe?

NET 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.

What is thread-safe in C#?

Thread safety is the technique which manipulates shared data structure in a manner that guarantees the safe execution of a piece of code by the multiple threads at the same time. A code is called thread-safe. If it is run concurrently without break function.

Why is thread safety important?

Thread safety simply ensures that when a thread is modifying or reading shared data, no other thread can access it in a way that changes the data. If your code depends on a certain order for execution for correctness, then you need other synchronization mechanisms beyond those required for thread safety to ensure this.


1 Answers

Local variables in that method will be per-call. I presume (don't know MVC) that the Controller instances are per-request, so any instance members would be safe.

But if there are any static or other shared members, then they are not safe.

like image 197
John Saunders Avatar answered Sep 23 '22 01:09

John Saunders