This is documentation for Mathematica 8, which was
based on an earlier version of the Wolfram Language.
View current documentation (Version 11.2)

SetSharedVariable

SetSharedVariable
declares the symbols as shared variables whose values are synchronized among all parallel kernels.
  • The unique value of a shared variable is maintained by the master kernel and every access on a parallel subkernel is synchronized through the master kernel.
  • Shared variables without a value evaluate to Null.
Make xs be a (global) shared variable:
Each subkernel increments the (global) shared variable value:
Without sharing, each subkernel has its own (local) copy of the variable:
Make xs be a (global) shared variable:
In[1]:=
Click for copyable input
Each subkernel increments the (global) shared variable value:
In[2]:=
Click for copyable input
Out[2]=
In[3]:=
Click for copyable input
Out[3]=
Without sharing, each subkernel has its own (local) copy of the variable:
In[4]:=
Click for copyable input
In[5]:=
Click for copyable input
Out[5]=
In[6]:=
Click for copyable input
Out[6]=
Share a single value:
Access a single element of a shared list efficiently:
Assign a new value to a shared variable:
Only the last assignment performed remains:
Change a single element of a shared list:
Update the value of a shared variable:
Append to a shared list:
Append to an element of a shared list:
Delayed values defined on the master kernel are evaluated on the master kernel:
Delayed values defined on a subkernel are evaluated on the kernel asking for the value:
Use shared variables to synchronize input and output in an infinite search:
Find such that is prime, until the computation is manually aborted:
Use a shared variable to signal all evaluations to stop as soon as a prime has been found:
Use a shared variable to hand out tasks to do:
Monitor the progress of a calculation, counting major calculation steps:
Return results as they are found:
Watch the results appear in real time:
$KernelCount is effectively a shared variable:
Separate read and write operations are not thread-safe:
Use atomic updates for synchronization:
Alternatively, use CriticalSection to make a whole code section atomic:
New in 7