CoreSystem.Lock

Value type synchronization classes.

  • SyncLock<T>: Lock method acquire lock on particular value (ID) and released as soon as code exits using block.
private static readonly SyncLock<long> sync = new SyncLock<long>();

using (sync.Lock(subscriber.SubscriberID))
{
     // Working exclusively for that subscriber ...
}

  • SyncValue<T>: GetObject returns unique object for the value (ID), it keeps object alive in the memory until application terminates. Use this class you if have very small number of unique IDs. 
private static readonly SyncValue<long> sync = new SyncValue<long>();

lock (sync.GetObject(subscriber.SubscriberID))
{
   
// Working exclusively for that subscriber ...
}

Both classes allows you to work exclusively for value types, but SyncLock acquire lock in ‘Lock’ method and must be used in using statement, because in ‘Dispose’ method; lock will be released and lock object for that particular value (in this case SubscriberID) will be destroyed if there are no more lock requests. In SyncValue class object created for particular value remain alive throughout application life cycle, although memory cost is very little therefore can be ignored.

Last edited Jun 14, 2012 at 10:37 AM by farazmasoodkhan, version 11

Comments

No comments yet.