Threads[Mutex] - Maple Help

# Online Help

###### All Products    Maple    MapleSim

Home : Support : Online Help : Programming : Multithreaded Programming : Mutex : Threads/Mutex/Create

Threads[Mutex]

 Create
 create a new mutex

 Calling Sequence Create()

Description

 • The Create command is used to create a new Mutex.  This command returns an integer identifier that is used to represent the mutex.
 • The Create command does not accept any arguments.
 • Once created, the mutex can be locked and unlocked using the Lock and Unlock commands.
 • When a mutex is no longer needed, it should be destroyed using the Destroy command.  This will release all the resources used by the mutex.
 • For more information on using mutexes, see the Mutex help page.

Examples

 > p := proc( m )    global count;    print( count );    count := count+1; end proc;
 ${p}{:=}{\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}}{:=}{\mathrm{count}}{+}{1}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end proc}}$ (1)
 > $\mathrm{count}≔1$
 ${\mathrm{count}}{:=}{1}$ (2)

Create ten threads running the p function.

 > $\mathrm{Threads}[\mathrm{Wait}]\left(\mathrm{seq}\left(\mathrm{Threads}[\mathrm{Create}]\left(p\left(m\right)\right),i=1..10\right)\right)$
 ${1}$
 ${2}$
 ${3}$
 ${4}$
 ${5}$
 ${6}$
 ${6}$
 ${8}$
 ${8}$
 ${10}$ (3)

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}{:=}{\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}}{:=}{\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}≔1$
 ${\mathrm{count}}{:=}{1}$ (5)
 > $m≔\mathrm{Threads}[\mathrm{Mutex}][\mathrm{Create}]\left(\right)$
 ${m}{:=}{1}$ (6)

Create ten threads running the new p function.

 > $\mathrm{Threads}[\mathrm{Wait}]\left(\mathrm{seq}\left(\mathrm{Threads}[\mathrm{Create}]\left(p\left(m\right)\right),i=1..10\right)\right):$
 ${1}$
 ${2}$
 ${3}$
 ${4}$
 ${5}$
 ${6}$
 ${7}$
 ${8}$
 ${9}$
 ${10}$ (7)

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

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

 See Also

## Was this information helpful?

 Please add your Comment (Optional) E-mail Address (Optional) What is ? This question helps us to combat spam