Note that there are some explanatory texts on larger screens.

plurals
  1. PODo ASP.NET developers really need to be concerned with thread safety?
    text
    copied!<p>I consider myself aware of the concepts of threading and why certain code is or isn’t “thread-safe,” but as someone who primarily works with ASP.NET, threading and thread safety is something I rarely think about. However, I seem to run across numerous comments and answers (not necessarily for ASP.NET) on Stack Overflow to the effect of “<strong><em>warning – that’s not thread-safe!</em></strong>,” and it tends to make me second guess whether I’ve written similar code that actually could cause a problem in my applications. [shock, horror, etc.] So I’m compelled to ask:</p> <p><strong>Do ASP.NET developers <em>really</em> need to be concerned with thread safety?</strong></p> <p>My Take: While a web application is inherently multi-threaded, each particular request comes in on a single thread, and all <em>non-static</em> types you create, modify, or destroy are exclusive to that single thread/request. If the request creates an instance of a DAL object which creates an instance of a business object and <a href="https://stackoverflow.com/questions/350502/initialize-a-collection-within-an-object/350585#350585">I want to lazy-initialize a collection within this object</a>, <em>it doesn’t matter</em> if it’s not thread-safe, because it will never be touched by another thread. ...Right? (Let’s assume I’m not starting a new thread to kick off a long-running asynchronous process during the request. I’m well aware that changes everything.)</p> <p>Of course, static classes, methods and variables are just the opposite. They are shared by <em>every</em> request, and the developer must be very careful not to have “unsafe” code that when executed by one user, can have an unintended effect on all others.</p> <p>But that’s about it, and thus thread safety in ASP.NET mostly boils down to this: Be careful how you design and use statics. Other than that, you don’t need to worry about it much at all. </p> <p>Am I wrong about any of this? Do you disagree? Enlighten me!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload