Online Help

All Products Maple MapleSim

The Threads package provides user level commands for using threads in Maple. Threads allow for multiple paths of execution to be running at the same time. This can allow Maple to perform a greater number of computations per second when running on a multi-core machine.

To access the Threads package you must be running the multithreaded Maple engine. Please see the multithreaded page for information on how to start multithreaded Maple engine and for more general information on the multithreaded Maple engine and its limitations. This example worksheet assumes that you are running a multithreaded kernel. To test this execute the following line

>

restart;

>

kernelopts( multithreaded );

true

(1)

If the command above returned false then you are not running the multithreaded kernel and this worksheet will not function properly.

Create Threads

The Task Programming Model

Note: The Task Programming Model is the preferred way to write a multithreaded program in Maple. Although the Threads:-Create method still exists, the Task Programming Model has significant advantages over the explicit thread programming required when using the Threads:-Create method. See examples/Task.

The Threads[Create] function is used to start a new thread. The first argument passed into Threads[Create] is an expression that will be evaluated in the new thread. Threads[Create] returns an integer that can be used to identify the thread. For example, the Threads[Wait] function can be used to suspend the execution of one thread until other threads have completed. A thread can also get its own identifier by calling Threads[Self].

A critical section is a block of code where a thread can access data that is global between threads. If multiple threads are both in a critical section at the same time, they could modify the global data at the same time. This could leave the global data in an invalid state. To protect critical sections, the Threads package provides mutexes. Mutexes allow a programmer to guarantee that only one thread may enter a critical section at a time. For more information see the Mutex help page.

The following example shows what can happen if multiple threads are allowed to enter a critical section.

It is very useful for threads to be able to control the execution of other threads. To allow for this, the Threads package provides condition variables. A condition variable allows a thread to put itself to sleep. Another thread is then able to wake that thread up so that it can continue executing. This can be useful in many situations. For example if one thread needs the results of a computation being performed in another thread, then it can wait on the condition variable until the result is available. For more details see the Condition Variable help page.

The following example shows the interaction between a single thread that is producing jobs (called the Producer) and multiple threads that are consuming the jobs (called Consumers). There are two condition variables used. The first, called cp, is used to allow the Producer thread to wait while there are jobs to be performed. When Consumers need more jobs they can signal the Producer to start producing more jobs. After signaling the Producer, a Consumer waits on the second condition variable, cc. Once the Producer has generated more jobs it can awaken Consumers by signaling on cc.

This example will show how mutexes and condition variables can be used to control access to shared resources. In this model we have N monkeys sitting around a circular table. Between two monkeys sitting beside each other is a computer monitor, thus there are N monitors. These monkeys are computer programmers who need to write code, unfortunately they will only write code if they can use both of the monitors they are sitting beside.

To model this situation, we will create an array of data structures to model the monitor. Each data structure has a state, which indicates if the monitor is being used, a mutex, which allows a thread to update the data structure, and a condition variable which is used for a thread to wait for a monitor to be come available.

Maplesoft™, a subsidiary of Cybernet Systems Co. Ltd. in Japan, is the leading provider of high-performance software tools for engineering, science, and mathematics. Its product suite reflects the philosophy that given great tools, people can do great things.