Locking by string. Is this safe/sane?

前端 未结 9 1639
-上瘾入骨i
-上瘾入骨i 2020-12-14 01:33

I need to lock a section of code by string. Of course the following code is hideously unsafe:

lock(\"http://someurl\")
{
    //bla
}

So I\'

9条回答
  •  死守一世寂寞
    2020-12-14 01:34

    Another option is to get the HashCode of each URL, then divide it by a prime number and use it as an index into an array of locks. This will limit the number of locks you need while letting you control the probability of a “false locking” by choose the number of locks to use.

    However the above is only worthwhile if it is too costly just have one lock per active url.

提交回复
热议问题