Sync Root

A Sync Root is a data structure that provides a global lock that can be shared by multiple services and applications over the Internet. When you have a shared data structure, sometimes you want to place locks on certain items to gain exclusive accesses to avoid conflicts. The Sync Root provides such a locking mechanism by using leases on a Windows Azure Blob.

Basics

To allocate a new lock collection named "mylocks", use the following code. You'll need to replace [your storage connection] with your Windows Azure Storage account connection string.
CloudSyncRoot syncRoot = new CloudSyncRoot("[your storage connection string]", "mylocks");

Then, to acquire a lock named "alock" for 15 seconds, use:
string lease = syncRoot.Accquire("alock", TimeSpan.FromSeconds(15));

You need to keep the returned lease it, which is needed to release the lock:
syncRoot.Release("alock", lease);

Remarks

  • The acquired lock expires after the given timespan.
  • A single-letter string will be uploaded to your blob storage for each lock. The lock is based on the leases on this blob. The blob is NOT removed when you release the lock. This is to improve the performance of repeatedly locking/unlocking the same lock.

Last edited Nov 4, 2013 at 12:42 AM by Haishi, version 1