Check out the new USENIX Web site. next up previous
Next: Flow Control Primitives Up: Flow Of Control Features Previous: Flow Of Control Features

Threads

Nickle contains a continuation-based thread system, implemented from scratch entirely in UNIX-portable C code at the byte-interpreter level. Thread scheduling utilizes real-time thread priorities with round-robin scheduling among equal-priority threads. Support is provided for thread synchronization via built-in semaphores or mutex variables.

Threads in Nickle are important for at least three reasons. First, some calculations are most naturally performed concurrently. Consider a simple generate-and-test example like the odd-number example of Figure 3. While in this simple example saving the generator state would be easy, in practice more complicated cases (such as for example a chess move generator) are much easier to write if the generation and testing are performed in separate threads.

Figure 3: An odd-number printer using threads.
\begin{figure}\begin{verbatim}import Semaphore;semaphore prod, cons;
int ...
...f(''%d\n'', i);
signal(prod);
}
Thread::join(t);
}\end{verbatim}\end{figure}

Secondly, prototyping of parallel algorithms is most easily performed using a language with concurrent features. The concurrency primitives provided by Nickle are very similar to those of low-level parallel systems, allowing prototyping of code like that of Figure 3 above for later translation to a truly parallel environment.

Finally, the construction and use of Nickle itself is made easier by the availability of threads. In particular, the combination of continuations and threads eases the implementation of debugging in the presence of exceptions. Normal command-line Nickle execution is handled by a thread separate from the command parser. When an unhandled exception occurs, the debugger can simply expose the thread state, including the exception continuation, to the user for inspection.


next up previous
Next: Flow Control Primitives Up: Flow Of Control Features Previous: Flow Of Control Features
Bart Massey 2001-04-19