How do I lock when the ideal scope of the lock object is known only at runtime?
Clash Royale CLAN TAG #URR8PPP How do I lock when the ideal scope of the lock object is known only at runtime? The requirement is fairly straightforward: I want to make sure multiple threads aren't modifying an object at the same time. The tricky part is that the object is coming from a factory whose implementation is unknown until runtime. It may return a singleton, may create a new instance each time, or may have a pool of shared instances. var thing = factory.Get(...); lock (???) { // modify thing } I understand it's not safe to lock on a public object that other code could potentially lock on, thus creating a possibility for deadlocks. In other words, I shouldn't lock (thing) . But ideally I want to lock on something with the same, known-only-at-runtime scope of thing . lock (thing) thing One potential solution I came up with is to use a ConcurrentDictionary of objects keyed by thing 's hashcode: ConcurrentDictionary thing private static ConcurrentDictionary...