How do I perform a lock
based on a specific string-key?
public void PerformUpdate(string key)
{
// TODO: Refine this, since they key-string won't
// be the same instance between calls
lock(key)
{
PerformUpdateImpl()
}
}
I've tried to keep lock-objects in a ConcurrentDictionary
, but somehow this doesn't hold up either.
Although this isn't concurrent (you can make it so), what about Dictionary<string,object>
.
Wouldn't this work?
dict.Add("somekey",new object());
lock (dict["somekey"]) {
...
}
This would allow a thread to lock a named instance of an object, which would I think do what you want.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With