SetSharedVariable
✖
SetSharedVariable
declares the symbols si as shared variables whose values are synchronized among all parallel kernels.
Details

- 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.
- SetSharedVariable has attribute HoldAll.
Examples
open allclose allBasic Examples (1)Summary of the most common use cases
Make xs be a (global) shared variable:

https://wolfram.com/xid/0bh0oifvcgpggm-n57t1
Each subkernel increments the (global) shared variable value:

https://wolfram.com/xid/0bh0oifvcgpggm-d9uenc


https://wolfram.com/xid/0bh0oifvcgpggm-bdvuu

Without sharing, each subkernel has its own (local) copy of the variable:

https://wolfram.com/xid/0bh0oifvcgpggm-o9v42l

https://wolfram.com/xid/0bh0oifvcgpggm-df9qc


https://wolfram.com/xid/0bh0oifvcgpggm-r1say

Scope (8)Survey of the scope of standard use cases
Assignment (3)

https://wolfram.com/xid/0bh0oifvcgpggm-japih9

https://wolfram.com/xid/0bh0oifvcgpggm-evd4bb

Assign a new value to a shared variable:

https://wolfram.com/xid/0bh0oifvcgpggm-dgjsfc

https://wolfram.com/xid/0bh0oifvcgpggm-k20jmh

Only the last assignment performed remains:

https://wolfram.com/xid/0bh0oifvcgpggm-cn0nbs


https://wolfram.com/xid/0bh0oifvcgpggm-pvu52o

https://wolfram.com/xid/0bh0oifvcgpggm-esmj3q

https://wolfram.com/xid/0bh0oifvcgpggm-r9li36

Parts of a shared variable (3)
Access a single element of a shared list efficiently:

https://wolfram.com/xid/0bh0oifvcgpggm-i89vqf

https://wolfram.com/xid/0bh0oifvcgpggm-jpocrz

Change a single element of a shared variable:

https://wolfram.com/xid/0bh0oifvcgpggm-5ibgne

https://wolfram.com/xid/0bh0oifvcgpggm-ricidu

https://wolfram.com/xid/0bh0oifvcgpggm-7xc9qs

Modify a part of a shared variable:

https://wolfram.com/xid/0bh0oifvcgpggm-sm8p7v

https://wolfram.com/xid/0bh0oifvcgpggm-e24wqh

Associations (2)

https://wolfram.com/xid/0bh0oifvcgpggm-m8bxpu

https://wolfram.com/xid/0bh0oifvcgpggm-49kcfw

If the key is a string, Key can be omitted:

https://wolfram.com/xid/0bh0oifvcgpggm-eqq9bd

Modify a value in an association:

https://wolfram.com/xid/0bh0oifvcgpggm-p0s7kp

https://wolfram.com/xid/0bh0oifvcgpggm-wcdsj5

Generalizations & Extensions (1)Generalized and extended use cases
Delayed values defined on the master kernel are evaluated on the master kernel:

https://wolfram.com/xid/0bh0oifvcgpggm-nbcrg

https://wolfram.com/xid/0bh0oifvcgpggm-i2ikwi

https://wolfram.com/xid/0bh0oifvcgpggm-jsi3o5


https://wolfram.com/xid/0bh0oifvcgpggm-bn4oww

Delayed values defined on a subkernel are evaluated on the kernel asking for the value:

https://wolfram.com/xid/0bh0oifvcgpggm-e88i3m

https://wolfram.com/xid/0bh0oifvcgpggm-kmadv

https://wolfram.com/xid/0bh0oifvcgpggm-kxfl1n


https://wolfram.com/xid/0bh0oifvcgpggm-ceo4n3

Applications (5)Sample problems that can be solved with this function
Use shared variables to synchronize input and output in an infinite search:

https://wolfram.com/xid/0bh0oifvcgpggm-gje3ls
Find such that
is prime, until the computation is manually aborted:

https://wolfram.com/xid/0bh0oifvcgpggm-ddeli7

Use a shared variable to signal all evaluations to stop as soon as a prime has been found:

https://wolfram.com/xid/0bh0oifvcgpggm-w537e

https://wolfram.com/xid/0bh0oifvcgpggm-2s6d0

Use a shared variable to hand out tasks to do:

https://wolfram.com/xid/0bh0oifvcgpggm-fdk4rt

https://wolfram.com/xid/0bh0oifvcgpggm-f10gsn


https://wolfram.com/xid/0bh0oifvcgpggm-3c84

Monitor the progress of a calculation, counting major calculation steps:

https://wolfram.com/xid/0bh0oifvcgpggm-yj41g

https://wolfram.com/xid/0bh0oifvcgpggm-bkx6ba

Return results as they are found:

https://wolfram.com/xid/0bh0oifvcgpggm-chy7ji

https://wolfram.com/xid/0bh0oifvcgpggm-crvvfb

Watch the results appear in real time:

https://wolfram.com/xid/0bh0oifvcgpggm-chzxg2

Properties & Relations (3)Properties of the function, and connections to other functions
$KernelCount is effectively a shared variable:

https://wolfram.com/xid/0bh0oifvcgpggm-dpb4sr


https://wolfram.com/xid/0bh0oifvcgpggm-eh7zw8

Shared variables are listed in $SharedVariables:

https://wolfram.com/xid/0bh0oifvcgpggm-g28wjc

https://wolfram.com/xid/0bh0oifvcgpggm-gz26v3

To share definitions of functions, use SetSharedFunction:

https://wolfram.com/xid/0bh0oifvcgpggm-pwjmsa

https://wolfram.com/xid/0bh0oifvcgpggm-wumyn9
Shared functions are effectively evaluated on the master kernel:

https://wolfram.com/xid/0bh0oifvcgpggm-kk9pqp

Possible Issues (3)Common pitfalls and unexpected behavior
The single-bracket notation for association access is not supported by shared variables:

https://wolfram.com/xid/0bh0oifvcgpggm-bqmq1n

https://wolfram.com/xid/0bh0oifvcgpggm-0ym672

Use the Part notation instead:

https://wolfram.com/xid/0bh0oifvcgpggm-x6eywn

Separate read and write operations are not thread-safe:

https://wolfram.com/xid/0bh0oifvcgpggm-kxo973

https://wolfram.com/xid/0bh0oifvcgpggm-fybf7m


https://wolfram.com/xid/0bh0oifvcgpggm-n1d1rr

Use atomic updates for synchronization:

https://wolfram.com/xid/0bh0oifvcgpggm-fguo0j

https://wolfram.com/xid/0bh0oifvcgpggm-dt6vib


https://wolfram.com/xid/0bh0oifvcgpggm-cgpwbj

Alternatively, use CriticalSection to make a whole code section atomic:

https://wolfram.com/xid/0bh0oifvcgpggm-dyz7v5

https://wolfram.com/xid/0bh0oifvcgpggm-hdb28c


https://wolfram.com/xid/0bh0oifvcgpggm-b7qr5n

Create a shared variable whose value is a large matrix:

https://wolfram.com/xid/0bh0oifvcgpggm-lbcte3
Accessing its elements causes the whole matrix to be transferred to the subkernels repeatedly:

https://wolfram.com/xid/0bh0oifvcgpggm-g60jkc

Use Unevaluated to allow the special code for parts of shared variables to see the variable:

https://wolfram.com/xid/0bh0oifvcgpggm-cawvvr

Wolfram Research (2008), SetSharedVariable, Wolfram Language function, https://reference.wolfram.com/language/ref/SetSharedVariable.html.
Text
Wolfram Research (2008), SetSharedVariable, Wolfram Language function, https://reference.wolfram.com/language/ref/SetSharedVariable.html.
Wolfram Research (2008), SetSharedVariable, Wolfram Language function, https://reference.wolfram.com/language/ref/SetSharedVariable.html.
CMS
Wolfram Language. 2008. "SetSharedVariable." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/SetSharedVariable.html.
Wolfram Language. 2008. "SetSharedVariable." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/SetSharedVariable.html.
APA
Wolfram Language. (2008). SetSharedVariable. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/SetSharedVariable.html
Wolfram Language. (2008). SetSharedVariable. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/SetSharedVariable.html
BibTeX
@misc{reference.wolfram_2025_setsharedvariable, author="Wolfram Research", title="{SetSharedVariable}", year="2008", howpublished="\url{https://reference.wolfram.com/language/ref/SetSharedVariable.html}", note=[Accessed: 30-April-2025
]}
BibLaTeX
@online{reference.wolfram_2025_setsharedvariable, organization={Wolfram Research}, title={SetSharedVariable}, year={2008}, url={https://reference.wolfram.com/language/ref/SetSharedVariable.html}, note=[Accessed: 30-April-2025
]}