Overview of the Threads:-Mutex Package
Calling Sequence
Description
List of Threads:-Mutex Package Commands
Examples
Threads:-Mutex:-command( arguments );
command( arguments );
The Threads:-Mutex package provides user-level commands for using mutexes in Maple. Mutexes are used to control access to data when multiple threads are executing.
A mutex is an object that can be in one of two states: locked or unlocked. A thread can lock a mutex that is unlocked. The same thread can later unlock the mutex. When a mutex is locked, any thread attempting to lock the mutex will pause its execution until the mutex is unlocked. Once the mutex is unlocked, a waiting thread will obtain the lock and continue.
If multiple threads are waiting on a locked mutex, and the mutex becomes unlocked, one of the threads will obtain the lock and continue. Any other threads will continue to wait until they are able to obtain the lock.
A typical usage for a mutex is controlling access to a data structure that is shared between multiple threads, for example, two threads processing jobs that are stored in a shared data structure. Unless the threads protect access to the structure, both threads might attempt to take the same job off the list. Worst still, the data structure might get corrupted due to the parallel manipulations.
To solve this problem using a mutex, associate a mutex with the data structure. Whenever a thread wants to manipulate the data structure, it must first acquire a lock on the mutex. Once it has the lock, the thread has exclusive access to the data structure and can manipulate it. Once the thread is finished with the structure, it unlocks the mutex and processes the job. If the second thread attempts to access the data structure while the first holds the lock, it will pause and wait for the mutex to become available.
The Create command is used to create a new mutex.
The Destroy command is used to free the resources associated with the mutex.
The Lock command is used to obtain the lock on a mutex.
The Unlock command is used to release the lock on a mutex.
p := proc( m ) global count; print( count ); count := count+1; end proc;
p≔procmglobalcount;print⁡count;count ≔ count+1end proc
count≔1
Create ten threads running the p function.
ThreadsWait⁡seq⁡ThreadsCreate⁡p⁡m,i=1..10
1
2
3
4
5
6
8
10
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≔procmglobalcount;Threads[Mutex][Lock]⁡m;print⁡count;count ≔ count+1;Threads[Mutex][Unlock]⁡mend proc
m≔ThreadsMutexCreate⁡
m≔1
Create ten threads running the new p function.
ThreadsWait⁡seq⁡ThreadsCreate⁡p⁡m,i=1..10:
7
9
Using a mutex allows you to control access to the shared variable. Thus each number will be printed only once.
ThreadsMutexDestroy⁡m
See Also
examples/Threads
help
module
Threads
Threads:-ConditionVariable
UsingPackages
with
Download Help Document