Go up to Implementation
Go forward to Garbage Collection

Thread Scheduling

By default, new threads are declared in a lazy state and pushed on a global stack. Initially, there is only one active thread, the program's main thread. A lazy thread in the global stack is executed on the following occasions only:  
Idling
An idle process finds no activated thread ready for execution. In this case, it takes a lazy thread from the global stack, allocates a thread space and switches control to the newly activated thread.
Waiting
An active thread needs the result of a thread which is still in lazy state. In this case, the active threads executes the code of the lazy thread itself.
Terminating
A thread is going to terminate but finds a lazy thread in the global stack. In this case, the thread continues execution with the code of the lazy thread.
Control is switched from one active thread to another only, if the time slice of a thread has ended or if the thread needs the result of another thread which has been already activated (by another process) but not yet terminated.

As an example, let us take a look at a program where the initial thread t_0 creates two threads and waits for their results in turn. Each new thread creates two other threads resulting in the tree of threads of depth 4 depicted in the following figure:

 

Assume that there are three processes executing the program. A possible execution schedule is then described by the following table:

Time P_0 P_1 P_2 Task Stack
0 t_0 t_2,t_1
1 t_1 t_2 t_6,t_4,t_5,t_3
2 t_6 t_3 t_5 t_{14},t_{12},t_8,t_{13},t_{11},t_7,t_4
3 t_{13} t_7 t_{11} t_{14},t_{12},t_8,t_4
4 t_{14} t_8 t_{12} t_4
5 t_4 t_{10},t_9
6 t_{10} t_9
At time 0, process P_0 executes thread t_0 which creates threads t_1 and t_2. t_1 is immediately taken by P_1 for execution as well as t_2 by P_2 such that P_0 becomes idle at time 1. However, t_1 and t_2 push t_3,t_5,t_4,t_6 on the thread stack such that at time 2 process P_0 starts execution of t_6. In the following time steps, all processes now create the corresponding thread subtrees and, by waiting for the respective results, execute the corresponding code. At time 5, process P_0 is the quickest to grab t_4 and is the only one to continue execution while the other processes idle. In the end P_1 and P_2 take the newly created t_{10} and t_9.

All in all, the 15 lazy threads of the tree are executed by only six real threads of which only four threads are active at the same time (including the one thread which is blocked waiting for the result). Above figure illustrates this fact by a "bubble" for each active thread (with the different shadings representing the different processes).


Wolfgang.Schreiner@risc.uni-linz.ac.at
Id: main.tex,v 1.10 1996/04/04 11:45:47 schreine Exp

Up Next