Data Sheet

dScript
dScript User Manual v2.15
Multi-threading
What is multi-threading?
Multi-threading is the ability to run many parts of your program at the same time. It is like
having a separate CPU for each part, each running its own little program. Each part or section
of your program is called a thread and starts at a label. dScript is a native multi-threading
compiler. Any number of threads may be created, up to the limit of available RAM memory.
Threads are created with the thread declaration at the beginning of your program, like this:
thread myFirstThread 1000
This tells the compiler that you are declaring a new thread called myFirstThread. The 1000
attribute means this thread will be triggered to run every 1000mS. Threads may run constantly
or in response to internal or external trigger events.
Somewhere in your program you will need to place myFirstThread as a label, this is the
starting point for your thread. You must make sure your thread is a continuous loop or self
terminates when it is finished. Here is an example for the thread declared above:
var mySecondsCounter
thread myFirstThread 1000
myFirstThread: mySecondsCounter = mySecondsCounter + 1
threadsuspend
threadsuspend will suspend thread execution until it is triggered again each second (1000mS).
threadsuspend is the end of the thread. Thread execution stops and will never continue after
this instruction. The next time it is triggered it starts again at its entry label. This very simple
thread therefore implements a counter which counts up by one each second.
dScript programs always have at least one thread. When a program starts it jumps to the label
"main". This is just another thread to dScript, and the only one to start running automatically.
All other threads you declare are created in the stopped state. They will not run until they are
started with threadstart. This will typically be done in “main”, but threads may be started from
any other running thread.
main: threadstart myFirstThread
do
… other instructions …
loop
As with all threads, main should loop forever (as shown) or suspend itself with threadsuspend.
Copyright © 2016, Devantech Ltd.
All rights reserved.
www.robot-electronics.co.uk
52