KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I have a producer/consumer process. The consumed object has an ID property (of type integer), I want only one object with the same ID to be consumed at a time. How can I perform this ? Maybe I can do something like this, but I don't like it (too many objects created while only one or two with the same ID a day can be consumed and the lock(_lockers) is a bit time consuming : private readonly Dictionary<int,object> _lockers = new Dictionary<int,object>(); private object GetLocker(int id) { lock(_lockers) { if(!_lockers.ContainsKey(id)) _lockers.Add(id,new object()); return _lockers[id]; } } private void Consume(T notif) { lock(GetLocker(notif.ID)) { ... } } enter code here NB : Same question with the ID property being of type string (in that cas maybe I can lock over the string.Internal(currentObject.ID)
Tags (comma-separated)
Save Edits
Cancel