Mathematica 9 is now available
THIS IS DOCUMENTATION FOR AN OBSOLETE PRODUCT.
SEE THE DOCUMENTATION CENTER FOR THE LATEST INFORMATION.
Mathematica > Systems Interfaces & Deployment > Parallel Computing > Resource Sharing in Parallel Computing > CriticalSection >
Mathematica > Core Language > Tuning & Debugging > Parallel Computing > Resource Sharing in Parallel Computing > CriticalSection >

CriticalSection

CriticalSection
locks the variables with respect to parallel computation, evaluates expr, then releases the .
  • 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.
Protect a block of code from interference by other threads:
Without protecting access to the update code, subkernels may overwrite results:
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]=
Lock variables can be of the form :
Separate read and write operations are not thread-safe:
Use CriticalSection to make a whole code section atomic:
Locks are acquired by writing the kernel's ID into the lock variable:
Shared functions can be used for synchronization in place of a critical section:
Less efficient is the use of a shared variable and critical section:
Lock variables must not have a value before being used in a critical section:
A deadlock can be broken with an abort:
The dining philosophers, deadlock-free:
New in 7
Ask a question about this page  |  Suggest an improvement  |  Leave a message for the team
Format:   HTML  |  CDF