Search code examples
azure-service-fabric

How to acquire a particular lock?


How can I acquire an Update, Exclusive lock? Article states that “a user can ask for an Update lock instead of the Shared lock” but I didn't find a way to specify type of lock.

Currently I have a dictionary that stores lists. My goal is: when someone only reads the list — let's take a snapshot and don't block anyone else. When somebody is going to modify the list — let's take an Exclusive lock or maybe Update lock (honestly I can't find a difference) and everybody else should wait before transaction commits, even reads should not be possible.

The detail that I'm failing to understand is: how to specify that if intention is only to read, than allow read of the collection, and when intention is to read and then write(we are not able to alter existing data in other way), then this transaction should wait all locks and only then successfully read and modify the collection.


Solution

  • There's an enum called LockMode that you can pass to some methods on reliable dictionaries and queues that allow you to specify the functionality desired. Afaik, the only methods that support it are TryGetValue and ContainsKey on dictionaries and TryPeek on queues. I've been confused by that article myself, as I was originally lead to believe that I could specify it per transaction (which would be awesome btw). At the moment your options with LockMode are rather limited.