CriticalSection[var,expr]
acquires the lock var for parallel computation, evaluates expr, then releases the lock var.
CriticalSection[{var1,var2,…},expr]
locks all variables vari simultaneously.
CriticalSection
CriticalSection[var,expr]
acquires the lock var for parallel computation, evaluates expr, then releases the lock var.
CriticalSection[{var1,var2,…},expr]
locks all variables vari simultaneously.
Details
- At most one instance of a critical section with the same lock variable is allowed to run concurrently on any parallel kernel.
- Variables used as locks should not have a value in the master kernel.
- WithLock[var,expr] is equivalent to CriticalSection[var,expr].
Examples
open all close allBasic Examples (1)
Protect a block of code from interference by other threads:
SetSharedVariable[xs];Clear[xslock];xs = 0;
ParallelDo[CriticalSection[xslock, xs = xs + 1], {10}];
xsWithout protecting access to the update code, subkernels may overwrite results:
xs = 0;
ParallelDo[xs = xs + 1, {10}];
xsScope (2)
Lock variables can be of the form sym[index]:
ParallelTable[CriticalSection[{locks[Mod[i, 2]]}, i], {i, 10}]Use two indexed lock variables to protect the source and destination indices of an update operation:
Clear[locks];size = 10; bins = ConstantArray[0, size];
SetSharedVariable[bins]ParallelDo[
With[{i = RandomInteger[{1, size - 1}]}, CriticalSection[{locks[i], locks[i + 1]}, bins[[i]]++;bins[[i + 1]]--]], {100}];
binsTotal[%]Applications (1)
Separate read and write operations are not thread-safe:
n = 0;SetSharedVariable[n]ParallelEvaluate[n = n + 1]nUse CriticalSection to make a whole code section atomic:
n = 0;SetSharedVariable[n];Clear[nlock]ParallelEvaluate[CriticalSection[nlock, n = n + 1]]nProperties & Relations (3)
Locks are acquired by writing the kernel's ID into the lock variable:
Monitor[ParallelEvaluate[CriticalSection[lock, Pause[0.5];$KernelID]], lock]Shared functions can be used for synchronization in place of a critical section:
mlist = {};
include[e_] := (mlist = Union[mlist, {e}];)
SetSharedFunction[include]ParallelDo[include[RandomInteger[10]], {10}]mlistLess efficient is the use of a shared variable and critical section:
slist = {};SetSharedVariable[slist];
Clear[lock]ParallelDo[e = RandomInteger[10];CriticalSection[lock, slist = Union[slist, {e}]], {10}]slistWithLock works the same way as CriticalSection does with symbols:
SetSharedVariable[xs];xs = 0;
ParallelDo[WithLock[xslock, xs = xs + 1], {10}];
xsPossible Issues (1)
Lock variables must not have a value before being used in a critical section:
Clear[lock]ParallelEvaluate[CriticalSection[lock, Print[$KernelID]]]The deadlock that occurs when the lock variable does have a value can be broken with an abort:
lock = 5;ParallelEvaluate[CriticalSection[lock, Print[$KernelID]]]Neat Examples (1)
The dining philosophers, deadlock-free:
rp := Pause[RandomReal[4]];PrintTemporary[Dynamic[Array[forks, $KernelCount]]];
Clear[forks];forks[_] := Null;
With[{nf = $KernelCount},
ParallelTable[
With[{i1 = i, i2 = Mod[i + 1, nf, 1]},
Do[Print[i, " thinking"];rp;Print[i, " hungry"];
CriticalSection[{forks[i1], forks[i2]}, Print[i, " eating"];rp],
{2}]],
{i, nf}]];See Also
Related Guides
Text
Wolfram Research (2008), CriticalSection, Wolfram Language function, https://reference.wolfram.com/language/ref/CriticalSection.html (updated 2020).
CMS
Wolfram Language. 2008. "CriticalSection." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2020. https://reference.wolfram.com/language/ref/CriticalSection.html.
APA
Wolfram Language. (2008). CriticalSection. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/CriticalSection.html
BibTeX
@misc{reference.wolfram_2026_criticalsection, author="Wolfram Research", title="{CriticalSection}", year="2020", howpublished="\url{https://reference.wolfram.com/language/ref/CriticalSection.html}", note=[Accessed: 13-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_criticalsection, organization={Wolfram Research}, title={CriticalSection}, year={2020}, url={https://reference.wolfram.com/language/ref/CriticalSection.html}, note=[Accessed: 13-June-2026]}