1

I have an asp.net website where I would like to prevent concurrent access to certain pieces of code. Since every page request will get a thread of its own - that might be a problem.

If this were only one piece of code - I'd lock it. However, there are actually several related methods. If one thread enters one of them - I'd like to prevent other threads from entering any of them.

How do I achieve that?

ispiro
  • 26,556
  • 38
  • 136
  • 291
  • If you really have to do it - just lock on some Application level object. – Eugene Podskal Jun 24 '14 at 19:36
  • So use a single lock to control access to all of them. Nothing says that a lock can't be used in multiple places. – Jim Mischel Jun 24 '14 at 19:37
  • have a look at the Lock Statement in C#: http://msdn.microsoft.com/en-us/library/c5kehkcz.aspx – Dylan Corriveau Jun 24 '14 at 19:37
  • @EugenePodskal Do you mean that if I `lock(commonObject)...` then all locks on that object will be locked at once without me having to do anything else? – ispiro Jun 24 '14 at 19:38
  • @JimMischel See previous comment. – ispiro Jun 24 '14 at 19:39
  • @DylanCorriveau I did before asking. I think it's written for those who already know the subject well. – ispiro Jun 24 '14 at 19:41
  • 2
    I'd be very cautious about using a blocking lock on a webserver... It's a recipe for Threadpool pressure, deadlocks and latency. How about its async equivalent? http://blogs.msdn.com/b/pfxteam/archive/2012/02/12/10266988.aspx – spender Jun 24 '14 at 19:41
  • @spender Why? Why is a webserver different than any other computer? – ispiro Jun 24 '14 at 19:43
  • Just saw that after I posted. @spender is correct though. just out of curiosity, what are you trying to lock? Is there a way you can do it asynchronously? As to your last comment. Its not that the server is different, its the way the two applications are run. Websites are not applications. Since websites are multithreaded by design (multiple people will be accessing a site), it really shouldn't have operations that are locking the site. – Dylan Corriveau Jun 24 '14 at 19:47
  • 3
    @ispro ...because your code is running in the ThreadPool, which is not designed for blocking scenarios. ThreadPool jobs should be short lived and never block. – spender Jun 24 '14 at 19:47
  • See this reasoning here. However if there is a need to have a lock on certain resource, well, a solution must be found. I think at the end we are all just curious as to what is the scenario for this need. http://stackoverflow.com/questions/781532/do-you-use-lock-in-your-web-applications – Ernesto Jun 24 '14 at 19:52
  • @DylanCorriveau There's some data in a file (not enough to justify sql) which must be read and updated. And I don't want one thread to change the data between another thread's read and write. – ispiro Jun 24 '14 at 19:53
  • @Ernesto See previous comment. – ispiro Jun 24 '14 at 20:00
  • Wouldn't it be possible then to create a single entry point to access the file resource? Then it would be one lock. – Ernesto Jun 24 '14 at 20:02
  • @Ernesto There are different types of updates. But even if I do combine them into one method - it seems that locks are bad on webservers anyway. Or did I misunderstand, and the problem is only when _multiple_ locks on the same object exist? – ispiro Jun 24 '14 at 20:05
  • I don't know if locks are bad entirety on a web server, I mean, what if you have to? Not something you would do on a daily basis but as I said, if there is a need to lock on a resource, then what else can you do? Now, to my point, maybe you need to write different update methods to change the file, but maybe you can also create one single point where you get a hold of that file. Something along those lines. – Ernesto Jun 24 '14 at 20:10

1 Answers1

2

Well, you could put a lock on some object that is accessible by all of the code blocks - ideally it would be an internal object (or private if all of the code is in one class) so that no external code could lock it and block your code:

public class Global : System.Web.HttpApplication
{
    internal static readonly object LockObject = new Object();
    ...
}

... meanwhile ...

lock(Global.LockObject)
{
   // code block 1 
}

... elsewhere ...

lock(Global.LockObject)
{
   // code block 2
}
D Stanley
  • 149,601
  • 11
  • 178
  • 240
  • Thanks. I didn't realize that what I'm looking for is already there. – ispiro Jun 24 '14 at 19:42
  • Here is a good point on the same issue and a very similar answer to this one: the static lock assumes it is monitoring a static resource. http://stackoverflow.com/a/9405218/641530 – Ernesto Jun 24 '14 at 19:59