1-3 Thread


A thread is a single control unit of a CPU process. Under the N64 operating system, the code for all threads exists and is processed in main memory (RDRAM). A thread is like a small sub-program. N64 game programs use threads, messages, events and tasks. The priority of the thread determines thread execution order. (The kinds of threads differ depending on the applications.)


1-3-1 Kinds of Threads


Each programmer is free to program each thread as is appropriate for a given game application. However, in most cases, a programmer creates the following kinds of threads:



1-3-2 State of the Thread



  • Running state

  • Only one thread in a game program can be executing at a given time. Therefore, among those threads in the "ready condition" state, the thread with the highest priority is executed.

  • Runnable state

  • A thread is in the ready condition if it is ready to begin executing as soon as it becomes the highest priority thread in the ready condition. A thread can move into the ready condition when its processing is interrupted by a higher priority thread or when it has been waiting (in the waiting condition) for resources and those resources become available.

  • Waiting state

  • A thread is in the queued state when it is waiting for a message or event. Upon receiving the message (or when the event occurs), the thread changes state to the execution state or the ready condition depending on its priority.

  • Stopped state

  • A thread in the halt condition has no standing in the execution schedule. That is, a halted thread does not automatically become a candidate for execution unless the program specifically places it in the ready condition. For example, a newly created thread is automatically placed in the halt condition. A thread can change anytime to the halt condition and the ready condition. Use the osStartThread() function to place a thread in halt condition. Use the osStopThread() function to place a thread in the ready condition

    The following illustration shows the relationships between the thread conditions:


    Figure 1-3-1 State of the Thread




    Considering the fact that all threads share RDRAM, the following points need to be kept in mind:

  • If you raise the optimization level by adding the -O option when you compile your code, it is possible that each thread variable will not be updated. Therefore, you have to use the volatile keyword when using a variable that is common to two threads (threads A and B for example).



  • 1-3-3 Necessity of the Idle Thread


    The idle thread is the lowest priority thread. This thread is executed when the CPU does not have any other threads to process. If this thread did not exist, the CPU could not do anything. Even an idle thread has an important role. Make sure your game program creates an idle thread.