Main Content

mps.sync.mutex

Create a persistence service mutex

Description

example

lk = mps.sync.mutex(mutexName,'Connection',connectionName,Name,Value) creates a database advisory lock object.

Examples

collapse all

First, create a persistence service controller object and use that object to start the persistence service.

ctrl = mps.cache.control('myRedisConnection','Redis','Port',4519);
start(ctrl)

Use the connection name to create a persistence service mutex.

lk = mps.sync.mutex('myMutex','Connection','myRedisConnection')
lk = 

  TimedRedisMutex with properties:

        Expiration: 10
    ConnectionName: 'myRedisConnection'
         MutexName: 'myMutex'

Input Arguments

collapse all

Name of persistence service mutex, specified as a character vector.

Example: 'myMutex'

Name of connection to persistence service, specified as a character vector.

Example: 'Connection','myRedisConnection'

Name-Value Arguments

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Before R2021a, use commas to separate each name and value, and enclose Name in quotes.

Example: 'Expiration', 10

Expiration time in seconds after the lock is acquired.

Other clients will be able to acquire the lock even if you do not release it.

Example: 'Expiration', 10

Output Arguments

collapse all

A persistence service mutex object. If you use Redis™ as your persistence provider, lk will be a mps.sync.TimedRedisMutex object. If you use MATLAB® as your persistence provider, lk will be a mps.sync.TimedMATFileMutex object.

Tips

  • A persistence service mutex allows multiple clients to take turns using a shared resource. Each cooperating client creates a mutex object with the same name using a connection to a shared persistence service. To gain exclusive access to the shared resource, a client attempts to acquire a lock on the mutex. When the client finishes operating on the shared resource, it releases the lock. To prevent lockouts should the locking client crash, all locks expire after a certain amount of time.

  • Acquiring a lock on a mutex prevents other clients from acquiring a lock on that mutex but it does not lock the persistence service or any keys or values stored in the persistence service. These locks are advisory only and are meant to be used by cooperating clients intent of preventing data corruption. Rogue clients will be able to corrupt or delete data if they do not voluntarily respect the mutex locks.

Version History

Introduced in R2018b