Multithreading in .net
CoderSource.net
Multithreading in .net - Article by oraclequest
Level: MediumType: Article
Rating: 5Page: 1 of 2

Date: 9/1/2005 12:00:00 AM

Environment: Windows, .Net

Multithreading in .net

Introduction

  This article is about muilti – threading concept in .NET explained using C# as programming language.

Muilti threading is an important aspect that needs a special attention while building very huge system, especially when there is a trade off between efficiency (how fast the system works?) and performance (how correctly the system works?). With appropriate usage of Multi threading very high performance application can be developed and build.
 

What is threading?

Each application/program that is running in the system is a process. Each process consist of one or more thread. These process can whole or part of the application/ program. A thread is a set of instruction or specific section of application that executes independently within the application or system. This can be also thought as the context in which code runs. So threads are basically light weight processes responsible for multitasking within a single application. Usually OS takes care of scheduling and execution of various threads.

What is Multi threading?

When the system is build on various threads to perform multiple tasks to achieve parallelism and improve the efficiency of the system. This is the ability of any system to do multiple tasks simultaneously. Threads are implement when there are situations in which more then one task at a time need to be completed.

Some of the benefits of threading are mentioned as follows:

  • Using threads the long duration tasks in application can be processed at the background
  • User interface can be made more appealing so that there is some task performed during various events like when the user clicks button for the processing some action a progress bar can be shown to show the progress of activity.
  • Speed of the application can be increased.
  • Threads can be useful in situations where there are gaps between tasks waiting for some event such as user input, file read, or receipt of data over the network. During the situation we can release various precious resources like memory usages etc

And many other advantages can be achieved using multiple threading.

Some Threading Model Background
There is lot of material available about various threading model, Let discuss some of the models that are commonly supported by win32 environment.

Single Thread Model 
In this type of thread model only one thread is used to run the process remaining process needs to wait . The other processes must wait for the current execution of the thread to complete. The main disadvantage of this model is system idle time and long time to complete of small task.

Apartment Thread Model (Single Thread Apartment Model)
In this model we can have multiple threads executing with in an application. In single threaded apartment (STA) each thread is isolated in a separate apartment underneath the process. Each process can have multiple apartments that can share data between these apartment. Here the application is responsible for defining the time duration of each thread execution under these apartment. All requests are serialized through the Windows message queue such that only a single apartment is accessed at a time and thus only a single thread will be executing at any one time. Advantage of this model over single threaded is that multiple tasks can be processed at one time on the users request instead of just a single user request. This model still lack in performance as it is serialized thread model, task will be performed one after another.

Multi Thread Apartment Model (Free thread Apartment Model)
The Multi Threaded Apartment (MTA) model has a single apartment created underneath the process rather than multiple apartments. This single apartment holds multiple threads rather than just a single thread. No message queue is required because all of the threads are a part of the same apartment and can share. These applications typically execute faster than single threaded and STA because there is less system overhead and can be optimized to eliminate system idle time. These types of applications are complex to program as thread synchronization should be provided as part of the code to ensure that threads do not simultaneously access the same resources. A race condition can occur for the shared resource. Thus it is completely necessary to provide a locking system. When locking is provided this can result in a deadlock of the system.

1 2

You Can Rate this Article, if you are Logged In      
 

More Links from CoderSource.net:

 
Refer to a Friend:

Your Details:

Name:     e-mail:

Friend Details:

Name:    e-mail:    


MENU
Home
MFC 
C++
.Net
WIN32
Programming
Forum
My Articles
Add to Google
Add to My Yahoo!
Welcome to Codersource.Net Login | Register | Faq  

SEARCH
Google
 

NOTES:


Thanks for visiting our CoderSource.net. This site will be improved with more articles. Interested visitors can also submit their articles through the Submit Article link.Your article will also be published after due consideration by the editor. 

© Copyright 2003. All rights on content reserved by CoderSource.net. Contact    About Us