Wednesday, August 20, 2008

.Net Multithreading

Concepts:
Multitasking: OS ability to execute multiple tasks::processes at a time. Earlier versions of windows used co-operative multitasking, where processes volunteer to release the processor. drawback-if the process doesn't release the CPU, other processes had no option but to wait endlessly. Today's OSs use pre-emptive multitasking wherein the process is involuntarily interrupted by OS and another process is executed. drawback-If 2 processes are sharig a common resource at the same time, one process may change the data, then be interrupted, allowing another process to again change the data, which can lead to issues bcs both processes assumed they had exclusive access to the data. This issue however be sorted by Synchronization.
Processes: A Process is a physically separated unit of memory & resources allocated to an application. Each process has it's own separation of data, execution code, and system resources.
Threads: An execution sequence in a process is called a thread. A thread is defined by the registers in use on CPU, memory stack used by the thread, and a container (Thread Local Storage - TLS) that keeps the track of the thread's current state.


  • Every process has atleast one thread.

  • A thread is really a pointer into the instruction stream portion of a process. The thread does not actually contain the instructions, but rather it indicates the current & future possible paths through the instructions determined by data and branching decisions.

Time Slices: The period that the process is allocated CPU in multitasking environment is known as Time Slice or Quantum. The period of this time slice is unknown to the programer and unpredictable.


Multithreaded processes: Initial mandatory thread in a process is called a Primary or Main thread. Further to this, we can split the process to share the time slice alloted to it. This can be done by spawning additional threads of execution within a process called - worker threads. and the concept is called Free Threading.



  • Apartment Threading Model: in which each time you want to do some background work, it happens in it's own process. ... hence called out-of-process threading.

  • Free Threading Model: multiple threads can spawn within a single process.

There is an even greater benefit to multithreading the applications if the computer has more than one processors.


The OS is responsible for determining which threads are executed on which processor. However, .NET platform does provide the ability to control which CPU a process uses. This is done by use of ProcessorAffinity property of the Process class in System.Diagnostics namespace. This setting is at process level and all threads of that process run on same CPU.


Interrupts: A Interrupt is a mechanism that causes the normally sequential execution of CPU instructions to branch elsewhere in the computer memory without the knowledge of the execution program. Windows determins the time slice and places an instruction in the current thread's execution sequene. The time slice can differ from system to system and even from thread to thread. Since the interrupt is placed in the instruction set, it is also known as Software Interrupt. Once the interrupt is placed, windows then allows the thread to execute. When the thread comes to the interrupt, Windows uses a special function Interrupt Handler to store the thread's state in the TLS. The current program counter ( the address of the currently executing instruction) for that thread, which was stored before the interrupt was recieved is then stored in TLS. The thread is then moved to the end of the thread queue for its given priority to wait its turn again.



  • The TLS is not saved to the queue.; it is tored in the memory of the process that contains the thread. A pointer to that memory is actually saved to the queue.

  • What happens when the thread decides tghat it does not need to use all of it's execution time slot? - Context Switching - A thread may decide that it needs to wait on a resource before it executes further, Therefore, it may yield its execution time to another thread. This is the responsibility of programmer and OS. The programmer signals the thread to yield. The thread then clears any interrupts that windows may have already placed in its stack. A Software interrupt is then simulated...Afterwards, it's just the same..

Thread Sleep: When a programmer takes the thread out of the thread execution queue for a specific priod of time, the thread is said to sleep. When a thread is put to sleep, it is again packed up into TLS, but this time, the TLS is placed in the Sleep queue instead of Thread execution queue. In order for a thread on sleep queue to run again, they are marked to do so with a different kind of interrupt called Clock Interrupt.


Thread Abort: Threads can be stopped explicitly as a request during the executio of another thread. When a thread is ended in this way, its called an abort. Threads also stop when they come to an end of their execution sequence. In any case, when a thread ois ended, the TLS is de-allocated.



  • Threads cannot be aaborted from within themselves.

Thread Priorities:



  • Scale of 0 to 31

  • 0 can be only set by System and means the thread is idle.

  • 1-15 can be set by users of Windows System

  • >16 by administrators only.

  • Threads inherit the priority of the processes in which they reside.

Lifecycle states of threads:



  • Aborted

  • AbortRequested

  • Background

  • Running

  • Stopped

  • StopRequested

  • Suspended

  • SuspendRequested

  • UnStarted

  • WaitSleepJoin

Few Thread class methods:



  • Sleep(), Interrupt()

  • Suspend(), Resume()

  • Abort()

  • Join()

Threading Opportunities:



  • Background Processes

  • Accessing External Resources

Threading Traps:



  • Execution order is not fixed

  • Threading in a loop can be dangerous

Working programatically with Threads:



  • Creating a thread:

using System;


using System.Threading;


public class SimpleThread {


public void SimpleMethod() {


int i = 5;


int x = 10;


int result = i * x;


Console.WriteLine("This code calculated the value " + result.ToString() + " from thread ID: " + AppDomain.GetCurrentThreadId().ToString());


}


public static void Main() {


// Calling the method from our current thread


SimpleThread simpleThread = new SimpleThread();


simpleThread.SimpleMethod();


// Calling the method on a new thread


ThreadStart ts = new ThreadStart(simpleThread.SimpleMethod);


Thread t = new Thread(ts); t.Start(); Console.ReadLine();


}


}




Application Domains:


No comments: