Threads to me are like the most bizarrely moody creatures on the Planet ".Net". Any programmer pushes the need to use the threads as far as possible, the only reason is that we know very little or very superficially about the threads.
So here is my attempt to simplify THREADS as neat and as simple as I can.
Lets begin with some basics first. What is thread? What is a thread in Processor (CPU) terms? What is a thread in terms of an Operating System, and then lastly, How we the .Net Framework handles and behaves with Threads.
THREADS & MULTITASKING:
THREAD: A thread is a unit of processing. Classic example of threading is, Men are generally single threaded and Women are multithreaded (No, it is nothing to do with intelligence, you know what I mean ;)) It is just that, the Human Male brain can handle single threads very easily (Like Win 3.1) this might be a reason for why guys are caught when they try to have more than one partners; and at the same time you don’t come to about women doing it ;).
Any ways, I guess I am drifting . . .!
So, in a nut shell, here is what a thread looks like when they approach the CPU for processing.
MULTITASKING: Multitasking is the simultaneous execution of multiple threads. Multitasking is generally handled by the Operating System, but these days the Processor manufacturers have taken this task at hand and have incorporated this feature in the processor itself (must have heard about Hyper Threading technology by Intel® ). I’ll cover the OS part of it here as it makes more sense.
The Multitasking soup comes in two flavors:
1. Co - Operative and
2. Preemptive.
The common and most sort after flavor being Preemptive.
1. Co-Operative Threads: Here each thread was responsible for surrendering the control to the processor so that it could process other threads, and thus CO-Operating with other threads, this basically meant that you have to explicitly write code to handle this task or else your program will crash the OS (Remember the Blue Screen). One poorly designed program can consume all of the CPU time for itself or cause the whole system to hang, consider this in a server…! Co-Operative Multitasking was applied in the Microsoft’s Windows 3.x and MAC OS (Prior to MAC OS X).
2. Preemptive Or Time - Sharing Threads: Here the processor is responsible for giving control to each thread for a certain amount of time in which the process would execute (famously quoted as ‘time-slice’, you see these terms make the concept sound big. . .). The Time-Slice will then be given to another process ignoring the fact that the previous process was completed or not. This is handled by Context Switch (Don’t worry, just another complicated word, I’ll take this up-shortly). Preemptive multitasking was implemented in the earliest version of UNIX in 1969.
Even in the preemptive approach two or more threads are not executed in parallel, it is just that they seem to do so as the switch between the processes is in micro or milliseconds. Parallel execution is possible only when there are two or more processors or Processors Core which share the same Thread Pool / Thread Queue.
Some Facts:
1. The hardware required to execute processes in a preemptive fashion was not very common till mid 1980’s.
2. The earliest preemptive multitasking OS available to home users was ‘Sinclair QDOS’ on the ‘Sinclair QL’, released in 1984.
3. Mac OS 9.x introduced an additional API with which the application could be programmed to use the preemptive or cooperative model hand in hand
The diagram to explain the threads above needs a little correction since our knowledge about thread has matured.
CONTEXT SWITCHING:
As I had mentioned earlier that Preemptive Multitasking is handled by Context Switching. and even more that when talk of Multitasking threads, it is necessary to mention Context Switching.
First of all, what is a context? Here, context is the environment with/in which a particular thread is being executed. It includes everything from state, data, operations to the mid way results to be used further in the calculations and the most important Program Counter.
The processor either uses a hardware timer or, is instructed by the OS to determine when a time-slice has ended for a given thread. Following steps occur in Context Switching,
1. When the hardware timer or the OS signals the interrupt, the processor saves all register values for the current thread onto the stack.
2. Processor then moves those register values from the stack to a data structure generally called a CONTEXT structure ( in geek world this CONTEXT data structure is known as Process Control Block (PCB), but for the simplicity of the Context here, we'll use the term CONTEXT structure). Did you notice the use of the word Context here…!
3. When the processor wants to switch back to a previously executing thread, it reverses this procedure and restores the registers from the CONTEXT structure associated with that thread.
This entire procedure is called context switching.
This all about at the threads at the Ground Level or if I may call so, ‘Ground 0’.
I’ll cover how to handle threads on 'Planet .Net' in my other post. Your comments / suggestions / queries are most welcome.
References:
1. The Digital Research Initiative
The University of North Carolina at Chapel Hill
URL: http://www.ibiblio.org/team/intro/unix/what.html
2. Technical Note TN2006: MP-Safe RoutinesURL: http://developer.apple.com/legacy/mac/library/technotes/tn/tn2006.html