WithLock

WithLock[File["path"],expr]

locks the file path, evaluates expr, then releases the file.

WithLock[LocalSymbol["name"],expr]

locks the local symbol name, evaluates expr, then releases the local symbol.

WithLock[var,expr]

locks the shared variable var, evaluates expr, then releases the shared variable.

Details and Options

Examples

open allclose all

Basic Examples  (2)

Prepare to access a file by many concurrent processes:

Protect concurrent access with a lock file in the same directory:

Use several parallel processes to try to write to the same file concurrently:

The data has been written (in a nondeterministic order) without interference among the processes:

Initialize a file by writing 0 into it:

Concurrently read the file and write an updated value:

All accesses have effectively been serialized:

Unprotected concurrent accesses may overwrite each other's values and even read partial content:

Scope  (3)

File Locking  (1)

Use a lock file to serialize a concurrent update operation:

LocalSymbol Locking  (1)

Use a local symbol as a lock:

Shared Variable Locking  (1)

Use a shared symbol as a lock in a parallel computation:

The past process to write the file wins; previous values are overwritten cleanly:

Options  (3)

ExpirationDate  (1)

A lock can be set to expire, to protect against the locking process being unable to release the lock:

A lock that was not released carries identifying information:

Once it has expired, the next WithLock to use it will break the lock:

PersistenceTime  (1)

A lock can be set to expire, to protect against the locking process being unable to release the lock:

A lock that was not released carries identifying information:

Once it has expired, the next WithLock to use it will break the lock:

TimeConstraint  (1)

If a lock is taken, WithLock will wait forever and will need to be aborted:

Give a finite timeout value to limit the time spent waiting for the lock to become available:

Properties & Relations  (2)

Using a shared variable in WithLock is equivalent to CriticalSection:

Update operations on local symbols are automatically protected from concurrency:

Possible Issues  (3)

If a file used as a lock already exists, the locking will hang:

This evaluation will not return until it is aborted:

Always use a nonexisting file as a lock:

If a local symbol used as a lock has a value, the locking will hang:

This evaluation will not return until it is aborted:

Always use a nonexisting local symbol as a lock:

Not all file systems support the required operation for local symbol locking:

File locks are supported in more cases:

Wolfram Research (2021), WithLock, Wolfram Language function, https://reference.wolfram.com/language/ref/WithLock.html.

Text

Wolfram Research (2021), WithLock, Wolfram Language function, https://reference.wolfram.com/language/ref/WithLock.html.

BibTeX

@misc{reference.wolfram_2021_withlock, author="Wolfram Research", title="{WithLock}", year="2021", howpublished="\url{https://reference.wolfram.com/language/ref/WithLock.html}", note=[Accessed: 28-July-2021 ]}

BibLaTeX

@online{reference.wolfram_2021_withlock, organization={Wolfram Research}, title={WithLock}, year={2021}, url={https://reference.wolfram.com/language/ref/WithLock.html}, note=[Accessed: 28-July-2021 ]}

CMS

Wolfram Language. 2021. "WithLock." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/WithLock.html.

APA

Wolfram Language. (2021). WithLock. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/WithLock.html