Multitasking

This collection of routines lets you create multiple, independent tasks. Each task has its own current statement being executed, its own subroutine call stack, and its own set of private variables. The local (file-level) and global variables of a program are shared amongst all tasks. When a task calls task_yield(), it is suspended, and the scheduler decides which task to execute next.

Multitasking is the simpler and easier cousin of Multithreading. Under multitasking, at most one task is actually running, and the programmer explicitly decides when task switching is safe, whereas under multithreading the threads normally run at the same time, and absolutely everything that can be shared by more than one thread must be covered by locks, which can even include things like the hidden reference counts on literal constants. In fact multithreading is widely considered the most difficult thing a programmer will ever have to master, whereas multitasking is normally quite easy.

Note that multitasking code which works perfectly well in a single-threaded environment is likely to fail the instant that any multithreading is added to the application. In truth, non-multitasking code is equally likely to fail the instant that any multithreading is added to the application, unless it is made thread-safe, at least in respsect of any and all data that may be referenced by multiple threads.

See also the taskwire.exw Windows demo program, and the news.exu demo [DEV??] for Linux and FreeBSD.

task_create - create a new task
task_schedule - schedule a task for execution
task_yield - yield control, so the scheduler can pick a new task to run
task_self - return the task id of the current task
task_suspend - suspend a task
task_status - the current status (active, suspended, killed) of a task
task_clock_start - restart the scheduler’s clock
task_clock_stop - stop the scheduler’s clock
task_list - get a list of all tasks