Search code examples
.netmultithreadingsynchronizationcluster-computingserver-farm

What to use instead of the "lock" statement when the code is running on multiple machines?


The lock statement ensures that one thread does not enter a critical section of code while another thread is in the critical section. However, it won't work if the workload is spread across a farm of servers (e.g. a few IIS servers + a load balancer).

Does .NET support such a scenario?
Is there any class that can be used to control the execution of a critical code section by threads running on multiple machines?

If not, is there any standard method of handling such problems?

This question was inspired by a discussion that started here but is not limited to SharePoint or ASP.NET.


Solution

  • If you have access to a centralized SQL Server instance, you can use it to act as a distributed lock coordinator and manage the application locks using the sp_getapplock and sp_releaseapplock stored procedures.

    Application Locks (or Mutexes) in SQL Server 2005