Lock - Maple Help

 Lock
 lock a mutex

 Calling Sequence Lock( mutexid )

Parameters

 mutexid - (integer) the mutex to lock

Description

 • The Lock command acquires the lock on the mutex with identifier mutexId for the current thread.  If no thread holds the lock on mutexId, then Lock will acquire the lock and return immediately.
 • If a thread already holds the lock, then Lock will wait until the mutex is unlocked.  Lock will then attempt to acquire the lock for the current thread.
 • To release the lock on a mutex, use the Unlock command.
 • When multiple threads are attempting to acquire the lock on a single mutex, there is no guarantee as to which thread will get the lock.  This means that it is possible for a thread to unlock a mutex and then lock it again while other threads are waiting to acquire the lock.
 • 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)$