2.2 Multithreading

There is not much for a kernel to do if a program only executes as a single thread. A minimum requirement of an RTK is to somehow support multithreading. Multithreading is the management of resources such that a program appears to execute more than one sequences of code `concurrently'.

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:


\begin{algorithmic}
\STATE Initialize
\WHILE{true}
\STATE wait for query
\ST...
...ery
\STATE formulate reply
\STATE transmit reply
\ENDWHILE
\end{algorithmic}

Likewise, the overall code to plan a path may appear as follows:


\begin{algorithmic}
\STATE initialize
\WHILE{true}
\STATE wait for change of ...
...course to reach destination
\STATE use new course
\ENDWHILE
\end{algorithmic}

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