One of the most powerful
features of the BasicX operating system is its ability to multitask.
This means it can run several tasks at the same time. It's possible to
divide large programs into smaller manageable tasks that can
communicate with each other.
There is more
information in the BasicX documentation about multitasking, this is a
brief tutorial that aims to outline a simple implementation of
multitasking and not re write the BasicX manual.
The following sample of
code simply turns the onboard red and green led's on and off but
hopefully demonstrates how multitasking is implemented in the BasicX
programming language.
Task 2 switches a global
variable, led, between 0 and 1. Task 1 looks at the global variable
and toggles the red and green led's according to its value.
Processing time is given
to other functions by calling the sleep command. We use the main
procedure to call the tasks using the call task command, and then we
enter an infinite loop which shares the processor time around all of
the active tasks.
dim led as
byte
dim stacktask1(1 to 32) as byte
dim stacktask2(1 to 32) as byte
sub main()
calltask "task1", Stacktask1
calltask "task2", Stacktask2
do
call sleep(2.0) 'do nothing and give all time to other tasks
loop
end sub
sub task1()
do
call sleep(0.0) 'allow other tasks to run
if(led=1)then 'turn on/off led
call putpin(25,1)
call putpin(26,0)
end if
if(led=0)then
call putpin(25,0)
call putpin(26,1)
end if
loop
end sub
sub task2()
do 'Set global variable LED
call sleep(1.0)
led=1
call sleep(1.0)
led=0
loop
end sub
Multitasking has some
draw backs to sequential programming; mainly multitasking software
generally uses more memory as each process is allocated its own stack.
Also, because the processor is frequently switching tasks software can
run slightly slower. However these are small limitations when compared
to the powerful system of multitasking.
|