CriticalSection

CriticalSection[{var1,var2,},expr]

locks the variables vari with respect to parallel computation, evaluates expr, then releases the vari.

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.

Examples

open allclose all

Basic Examples  (1)

Protect a block of code from interference by other threads:

In[1]:=
Click for copyable input
In[2]:=
Click for copyable input
Out[2]=

Without protecting access to the update code, subkernels may overwrite results:

In[3]:=
Click for copyable input
Out[3]=

Generalizations & Extensions  (1)

Applications  (1)

Properties & Relations  (2)

Possible Issues  (1)

Neat Examples  (1)

See Also

SetSharedVariable  ParallelSubmit  WaitAll

Introduced in 2008
(7.0)