I have a WCF service and an resource with records (having IDs to identify them). I want that only 1 ID can be accessed simultaneously – so i have written a little resource helper:
public sealed class ConcurrencyIdManager
{
private static object _syncRootGrant = new object();
private static List<int> _IdsInUse = new List<int>();
... // singleton
public void RequestAndWaitForIdGrant(int id)
{
lock (_syncRootGrant)
{
while (_IdsInUse.Where(i => i == id).Count() != 0)
{
Monitor.Wait(_syncRootGrant);
}
_IdsInUse.Add(id);
}
}
public void ReleaseGrantForId(int id)
{
lock (_syncRootGrant)
{
_IdsInUse.Remove(id);
Monitor.PulseAll(_syncRootGrant);
}
}
So in my WCF service i have
public void UpdateMySpecialEntity(Entity foo)
{
ConcurrencyIdManager.Instance.RequestAndWaitForIdGrant(foo.Id);
try {
// do something with the entity foo
}
finally { ConcurrencyIdManager.Instance.ReleaseGrantForId(foo.Id); }
}
Is the implementation correct so far? 🙂
If am reading your notes right, you want id’s 3 4 and 5 to edit simultaneously, but two threads with id 5 to block and wait for each other.
In that case use a concurrent collection of lock objects and use a simple lock on the object for that Id.
e.g. in pseudo c#