Will lock() statement block all threads in the proccess/appdomain?

Posted by MikeJ on Stack Overflow See other posts from Stack Overflow or by MikeJ
Published on 2010-06-08T12:58:14Z Indexed on 2010/06/08 13:02 UTC
Read the original article Hit count: 187

Filed under:
|
|

Maybe the question sounds silly, but I don't understand 'something about threads and locking and I would like to get a confirmation (here's why I ask).

So, if I have 10 servers and 10 request in the same time come to each server, that's 100 request across the farm. Without locking, thats 100 request to the database.

If I do something like this:

private static readonly object myLockHolder = new object();
if (Cache[key] == null)
{
   lock(myLockHolder)
   {
      if (Cache[key] == null)
      {
         Cache[key] = LengthyDatabaseCall();
      }
   }
}

How many database requests will I do? 10? 100? Or as much as I have threads?

© Stack Overflow or respective owner

Related posts about c#

Related posts about ASP.NET