>

p := proc( m )
global count;
print( count );
count := count+1;
end proc;

${p}{\u2254}{\mathbf{proc}}\left({m}\right)\phantom{\rule[0.0ex]{0.5em}{0.0ex}}{\mathbf{global}}\phantom{\rule[0.0ex]{0.5em}{0.0ex}}{\mathrm{count}}{\;}\phantom{\rule[0.0ex]{0.5em}{0.0ex}}{\mathrm{print}}{}\left({\mathrm{count}}\right){\;}\phantom{\rule[0.0ex]{0.5em}{0.0ex}}{\mathrm{count}}{\u2254}{\mathrm{count}}{\+}{1}\phantom{\rule[0.0ex]{0.5em}{0.0ex}}{\mathbf{end\; proc}}$
 (1) 
>

$\mathrm{count}\u22541$

${\mathrm{count}}{\u2254}{1}$
 (2) 
Create ten threads running the p function.
>

$\mathrm{Threads}\left[\mathrm{Wait}\right]\left(\mathrm{seq}\left(\mathrm{Threads}\left[\mathrm{Create}\right]\left(p\left(m\right)\right)\,i=1..10\right)\right)$

Without mutexes the same value may be printed multiple times. (You may have to execute this command multiple times to see this occur.)
>

p := proc( m )
global count;
Threads[Mutex][Lock]( m );
print( count );
count := count+1;
Threads[Mutex][Unlock]( m );
end proc;

${p}{\u2254}{\mathbf{proc}}\left({m}\right)\phantom{\rule[0.0ex]{0.5em}{0.0ex}}{\mathbf{global}}\phantom{\rule[0.0ex]{0.5em}{0.0ex}}{\mathrm{count}}{\;}\phantom{\rule[0.0ex]{0.5em}{0.0ex}}{\mathrm{Threads}}{\[}{\mathrm{Mutex}}{\]}{\[}{\mathrm{Lock}}{\]}{}\left({m}\right){\;}\phantom{\rule[0.0ex]{0.5em}{0.0ex}}{\mathrm{print}}{}\left({\mathrm{count}}\right){\;}\phantom{\rule[0.0ex]{0.5em}{0.0ex}}{\mathrm{count}}{\u2254}{\mathrm{count}}{\+}{1}{\;}\phantom{\rule[0.0ex]{0.5em}{0.0ex}}{\mathrm{Threads}}{\[}{\mathrm{Mutex}}{\]}{\[}{\mathrm{Unlock}}{\]}{}\left({m}\right)\phantom{\rule[0.0ex]{0.5em}{0.0ex}}{\mathbf{end\; proc}}$
 (4) 
>

$\mathrm{count}\u22541$

${\mathrm{count}}{\u2254}{1}$
 (5) 
>

$m\u2254\mathrm{Threads}\left[\mathrm{Mutex}\right]\left[\mathrm{Create}\right]\left(\right)$

Create ten threads running the new p function.
>

$\mathrm{Threads}\left[\mathrm{Wait}\right]\left(\mathrm{seq}\left(\mathrm{Threads}\left[\mathrm{Create}\right]\left(p\left(m\right)\right)\,i=1..10\right)\right)\:$

Using a mutex allows you to control access to the shared variable. Thus each number will be printed only once.
>

$\mathrm{Threads}\left[\mathrm{Mutex}\right]\left[\mathrm{Destroy}\right]\left(m\right)$
