Main Content

mps.sync.mutex

Create a persistence service mutex

Description

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

example

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

example

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'

Expiration time in seconds after the lock is acquired, specified as a positive integer.

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