Threads introduction
Previous  Next

Although  this picture is somewhat simplified you can view programs running on a computer as a set of separate processes each of which comprises one or more threads.

A process may be an application, such as the Basic4PPC IDE or one of the compiled applications it produces. Processes are generally separate from one another, do not share memory space, and are normally ignorant of each others' existence.

Each process consists of one or more threads. A thread is a separate flow of program execution and threads within a process share memory space and so can share global variables and program code.

Threads and processes are managed by the Operating System and at any one time there may be dozens of threads in existence. Many of these belong to and are being used by the Operating System itself, others represent application programs. The Operating System shares the computer amongst these threads by letting each one run for a short while, called a timeslice, and then suspending it to let another thread take its turn. Each thread has a priority and higher priority threads take precedence over lower priority ones when it comes to the amount of processor time they are allocated.

Basic4PPC applications have only one thread, the main or GUI (Graphical User Interface) thread that, not surprisingly, handles the user interface (and everything else). Using this library you can create additional threads that operate (apparently) in parallel alongside each other and the GUI thread.

Threads can be useful to keep the user interface alive while performing a long sequence of computations or while waiting for something to happen. A long running operation can be launched as a thread and ignored until it finishes when it may signal back that it has done so by setting a flag or causing an event. A poll for something to happen may be packaged into a thread and set to run independently, again signalling activity by setting flags and causing events.