Locking by string. Is this safe/sane?

前端 未结 9 1636
-上瘾入骨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:41

    Now I have the same issue. I decided to use simple solution: to avoid deadlocks create a new string with fixed unique prefix and use it as a lock object:

    lock(string.Intern("uniqueprefix" + url))
    {
    //
    }
    

提交回复
热议问题