Friday, February 22, 2008

Multithreading

The thread part of multithreading goes back to the kernel which is the heart of the operating system. The kernel controls the interaction between the hardware and the software. Keeping this in mind, processes are also included in the kernel. The operating system assigns the resources to the processes and they include the device handles, memory, file handles, and windows.
Threads are contained in each process. There can be as few a one or as many as the system needs to complete a required task. These threads can be run in parallel on some systems and as single operations on other systems. It's all considered multithreading. On single processor systems, threads are run one at a time but are sun so quickly the user is unaware of this fact. With the new dual core and quad core processors, threads can be run simultaneously on each core.
Multithreaded programs can run faster on systems with these new CPU's since the programs can be divided into several tasks running at the same time. Unfortunately, programs written to take advantage of this capability have to be constructed in such a way that the threads do not attempt to use the same resources at the same time. This can result in bus contentions or deadlock issues.
Several types of multithreading are in use today. Block multithreading consists of a single thread running until it's blocked. If a call to a memory location that is not in the cache is to be made, it may take several CPU cycles to retrieve the data. In this case, that thread is blocked and the thread processor could allow another thread to run in its place until the memory fetch is complete. This causes the hardware to switch register sets and adds time to the execution of the thread.
Interleaved multithreading reduces the number of CPU cycles down to one thread switch per CPU cycle. In this case the thread processing time is considerably less and each thread is executed separately from on another.
Simultaneous multithreading goes one step further. Each thread consists of multiple instructions per CPU cycle. These threads still contain program counters and are used in superscaler processors.
Hardware and software designers are always attempting to take advantage of the thread scheduler capabilities. The most efficient design would achieve the most thread instructions issued in the least number of CPU cycles while avoiding thread blocking altogether.

No comments: