Typical programs start by performing some initial tasks, continue with additional tasks, and then complete their execution with some final work. A short example is:
- Ask the user to input to numbers, “a” and “b”.
- Multiple the numbers to get a result c = a * b.
- Print the result in a format like “The result of a times b = c”, such as “The result of 2 times 5 is 10”.
- Exit program.
This type of program is called a “straight-line program.” It is called that since the path through the code goes one direction, and only one task is needed. Although this sample is very simple, straight-line programs can actually be significantly more complex, yet still only needs one actual task, or “thread.” For another example, consider a program that does more interaction with the user, such as:
- Initialize some variables used throughout the program
- Create a “loop” (repetitive set of steps) that does the following:
- Display a contact for to collect a name, address, e-mail and phone numbers with an OK and CANCEL button
- Wait for the user to fill out the form and press OK, or for the user to press CANCEL
- When OK is pressed:
- Extract the data entered into a set of variables.
- Do any examination of the variables for correctness. If correct:
- Create a database record and write the variables into the record, and record the record in your contact database.
- Loop back to display an empty form and continue the process.
- If any field is invalid:
- Pop up an error message explaining what field needs to be fixed
- When the user acknowledges the error, place the input cursor within the error’ed field so the user can correct it
- If the user pressed CANCEL, break out of the loop
- If there is no other work to do, simply close the program
As you can see from both examples, straight-line programs do one step at a time, with no need to perform other tasks while your main program is running. the only waiting done is for the user to press a button directing what you want to do next. While simple, many useful programs can be created using straight-line programming techniques.
However, to make much more interesting programs, such as programs that might have to interact with the internet, you will want to build a program that act like several tasks are executing at the same time, or multitasking. The next few articles in the series will introduce different techniques for building multitasking programs, each with a basic example. The first and simplest form of multitasking uses timers.
Before we get into some forms of multitasking, lets clarify some terminology:
- Task – A sequence of steps that typically accomplishes one goal, such as a task the downloads a file from the internet
- Thread – A programming construct that simply represents an independent path of execution within a program. Each CPU “core” can execute a single thread at a time.
- Multitasking – Programs that use multitasking will appear to be performing more than one task at a time, which typically, but not necessarily, requires a single thread for each task.
- Multi-threading – A type of multitasking where the program launches multiple threads, each appearing to run at the same time. If a CPU only has one core, then only one thread can run at a time – but the CPU switches between threads when necessary.
