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

后端 未结 5 2115
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-13 10:02

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

5条回答
  •  后悔当初
    2021-02-13 10:36

    There is nothing in .Net that can natively support cross-machine locking.

    The standard method is to move the responsibility for such things to a single place, possibly a web service (not load balanced!), so it can still be called from multiple locations. Or alternatively, defining a single resource, that's accessible by all (eg database) and using this as the single resource to acquire (eg write key in locks table, if key exists lock cannot be acquired until row removed)

提交回复
热议问题