Reader Level:
ARTICLE

Basic concepts of Threading: Part I

Posted by Sonu Chauhan Articles | Multithreading in C# January 27, 2006
In this article, we will look at the basic concepts of threading in C# and .NET.
  • 0
  • 0
  • 48534

Multithreading

 

Multithreading is not a new concept in software development. Before looking into how multithreading is implemented in .NET, let us have a brief look at what threads are and why they are important. 

 

What are threads?

 

A thread can be defined as a sequence path of execution for a program. Multithreaded program contain two or more parts that can run concurrently. Each part of such a program is called a Thread.

 

Let us have a brief look at one of the important concept called Time Sharing. A computer System can give an expression that several thing r done simultaneously by running each process for a few milliseconds, then saving it state and switching over to the next process and so on. Threads simply extent that concept from switching between several different programs to switching between several different functions executing simultaneously within a single program. A thread in not restricted just to one function. Any thread in a multithreaded program can call any series of methods that could be called in a single threaded program.

 

When and operating System switches from running one process to running another process for its time slice, there is quite costly over head of saving the program's state. When the system switches from running one of our thread to running another of our threads, a lot over ahead context switch within the same address space is done. Threads can actually achieve the counterintuitive result of making a program run faster, even on uniprocessor hardware. This occur when there are calculation steps that no longer have to wait for the out put to complete, but can run while the I/O is taking place.

Threads are the way we get several things to happen at once in a program. Why is this good idea? In an unthreaded program only one thing happens at a time. Threads allow a program to do one thing at a time. There are three reasons why would we do this.

 

 

1.  We have interactive program that never "Go Dead" on the user. We might have one thread controlling the and responding to a GUI, while another thread carries out the tasks and computations requested, white a third thread does file I/O, all for the same program. This means that when one part of the program is blocked waiting on some other resource, the other threads can still run and are not blocked.

 

2.  Some programs are easier to write if we spilt them into threads. The classic example is the client-server. When a request comes in from a client, it is very convenient if the server can spawn a new thread to process that one request. The alternate to have one server program try to keep track algorithmically of the state of each client request.

 

3.   Some programs are amenable to parallel processing. Writing them as threads allow the code to express this. Example includes some sorting and merging algorithms, some matrix operations, and many recursive algorithms.

 

Coming soon:-

 

Part 2 -- Utilization of System.Threading NameSpace, Life Cycle of Methods (Killing a Thread, Pausing a Thread, Suspending a Thread, Resume a suspended Thread)

 

Part 3 -- Sample of MulthiThreaded Application using C#.Net.

 

Part 4 -- Some Important Properties and Methods of Threads.

COMMENT USING

Trending up