Reputation: 736
I need to synchronize a critical section, but only if the code is working with the same data (determined by IDs of objects). That's why I implemented a lock object container which dynamically creates lock objects based on a string identifier. Lock objects are stored in a dictionary and reused if somebody asks for an object with the same key.
The problem is that there are millions of combinations of the locking key and lock objects currently stay in the dictionary and consume memory forever.
How to design such dictionary, so that it removes unneeded lock objects and thus frees memory?
My ideas:
Do I have any other options?
Upvotes: 0
Views: 105
Reputation: 1614
I know the question is very old, but in case someone stumbles upon this and has the same issue, take a look at AsyncKeyedLock which solves the problem. You can use:
using (await _locker.LockAsync(myObject.Id))
{
...
}
Upvotes: 1