Threads and Semaphores: 3.2 GEOS Multitasking: Context Switches

Up: GEOS SDK TechDocs | Up | Prev: 3.1 GEOS Threads | Next: 3.3 Thread Scheduling

Context switches are triggered in two ways under GEOS. The first is a timer or other hardware interrupt. The second occurs when the thread reaches a point where it cannot continue right away, such as when the thread exits or when it attempts to access a locked resource.

The PC hardware generates a timer interrupt sixty times per second. The time between timer interrupts is called a tick . Each thread is allowed to run for a specified number of ticks before the timer interrupt routine will transfer control to a different thread. This number of ticks is the same for all threads; it is called a time slice .

When a thread begins its turn, GEOS sets a counter to the number of ticks in a time slice. At each timer interrupt, GEOS decrements the counter. If the counter has not reached zero, control is immediately returned to the running thread. When the counter reaches zero, GEOS checks to see if some other thread has reached a higher priority than the current thread. If so, the current thread is placed in the system's list of runnable threads (called the run queue ), and the highest priority thread begins running. Otherwise the current thread gets to run for another time slice.

Sometimes a thread will try to access a system resource (or shared data object) which is currently in use by another thread. When this happens, the thread must wait until the desired resource is available. The thread is placed on a queue, and a new thread is selected from the run queue. Every thread that is not currently running is either in the run queue (waiting to be executed) or in another queue waiting for a needed resource to become available. When the resource becomes available, the thread is moved to the run queue and is ready to be run again. This process is described in greater detail in Synchronizing Threads .


Up: GEOS SDK TechDocs | Up | Prev: 3.1 GEOS Threads | Next: 3.3 Thread Scheduling