Creating Dynamic Locks at Runtime in ASP.NET

前端 未结 2 1477
有刺的猬
有刺的猬 2021-02-08 15:21

Are the following assumptions valid for this code? I put some background info under the code, but I don\'t think it\'s relevant.

Assumption 1: Since th

相关标签:
2条回答
  • 2021-02-08 16:05

    lock only has a scope of a single process. If you want to span processes you'll have to use primitives like Mutex (named).

    lock is the same as Monitor.Enter and Monitor.Exit. If you also do Monitor.Enter and Monitor.Exit, it's being redundant.

    You don't need to lock on read, but you do have to lock the "transaction" of checking if the value doesn't exist and adding it. If you don't lock on that series of instructions, something else could come in between when you check for the key and when you add it and add it--thus resulting in an exception. The lock you're doing is sufficient to do that (you don't need the additional calls to Enter and Exit--lock will do that for you).

    0 讨论(0)
  • 2021-02-08 16:12

    Your current solution looks pretty good. The two things I would change:

    1: UnlockOnValue needs to go in a finally block. If an exception is thrown, it will never release its lock.

    2: LockOnValue is somewhat inefficient, since it does a dictionary lookup twice. This isn't a big deal for a small dictionary, but for a larger one you will want to switch to TryGetValue.

    Also, your assumption 3 holds - at least for now. But the Dictionary contract makes no guarantee that the Keys property always returns the same object. And since it's so easy to not rely on this, I'd recommend against it. Whenever I need an object to lock on, I just create an object for that sole purpose. Something like:

    private static Object _lock = new Object();
    
    0 讨论(0)
提交回复
热议问题