We all know that a processor (espcially one with only one ALU) cannot truly perform operations in parallel. So what is the point to ```pretend'' parallelism? The answer is quite simple: to make coding easier. It is the same argument to write programs in high level lagnagues instead of machine code, even though everything must be translated to machine code before a processor can execute the code.
Let us consider an program that runs on a robotic system. To ensure reliability, it is usual that we have the following tasks to perform:
Each of these tasks is, by itself, a sequence of operations. For example, the overall code to maintain communication is as follows:
Likewise, the overall code to plan a path may appear as follows:
As you can see, each individual task is an infinite loop. This makes sense because there is no logical end to each task. Without multithreading, it becomes difficult to translate the pseudocode to actual code. This is because there is no way to express the fact that these individual loops be performed ``in parallel''.
With multithreading, each logical task is implemented by a top level subroutine. The top level subroutines can call other subroutines. In other words, we write the following functions for our example:
void planPath(void)
{
while (1)
{
// ...
}
}
void communicate(void)
{
while (1)
{
// ...
}
]
Copyright © 2008-10-25 by Tak Auyeung