Multi-Threading with C#

By Rosen Kehayov

A small handout for beginners in multithreading in .NET environment

Introduction
In order to understand completely what is a thread, how and when to useit we should first see how Windows operating system works."Windows is a preemptive multitasking operation system." (Aitken,Peter; 2002)

Preemptive:
When the operation systemdecides how much time a task will take to be executed this is apreemptive multitasking operation system. While when the task itselfdecides for how long to execute this is cooperative multitaskingoperation system. So the difference between preemptive and cooperativeoperation system is in the object, which decides for how long a task tobe executed. (Aitken, Peter; 2002)

Multitasking:
A system with only one CPUcan work over only one task at a time. The memory space, where a givenapplication is executed is called – process. A Process is the memoryset aside for an application to be executed in. Within this process thething, which is really executed is the thread. The operation systemallocates CPU's time to the thread. So a process contains at least onethread but the process can contain many threads, which can be executed"simultaneously" by sharing the CPU. The illusion of simultaneousexecution comes when the operation system divides the CPU's time amongall the running processes. (Hinton, Ben; 2002)

Thread:
So a thread is the compound unit ofa process, which the operating system allocates CPU's time to. Aprocess always has at least one thread running, but it also can havemany threads running, which share the CPU's time. (Hinton, Ben; 2002)

Priorities
The operation system decides howto allocate CPU's time to the processes according to the process'spriority level. In Windows 2000 there are four priority levels, whichcan be assigned to processes:- Real time – highest priority. The processes with real time prioritycan interrupt all other processes with lower priority. This type ofpriority is reserved for processes which must not be interrupted inorder to be done well like streaming video or applications with complexgraphics.- High priority: for processes that should be executed immediately.Such as Windows Task List, which should be fired immediately after theuser request. The high priority processes can be interrupted only byprocesses with real time priority and can preempt all processes withlower priority.- Normal priority: for normal applications, which do not have anyspecial CPU's time allocations.- Idle priority: for processes, which are done only when the CPU isidle, such as Screen saver.The above-mentioned priority levels exist in Windows2000 and in .NETthey are different but the logic behind them is the same. The prioritylevels in .NET will be discussed later in this article. (Aitken, Peter;2002)

Types of Threading in Windows
There arethree basic types of threading in Win32 environment:- Single Threading – There is only one thread in the application, andit has to do all the work. That thread has all the space allocated forthe process.- Apartment Threading – There are multiple threads within theapplication. The application defines when and for how long a threadshould be executed. Each thread has an assigned space within the spaceallocated for the application and the threads do not shared thatresource.- Free Threading – There are multiple threads within an application andthese threads do share resources among. Different threads can call thesame method and property at the same time.The apartment model is more efficient than the single threading becausethe work is divided among multiple objects, while the free model is thefastest and the most efficient. But the free model hides many risks inthe same time. Because of the sharing of resources among multipleobjects, much more attention must be paid over synchronizing thatactivities and not allowing mutual exclusive changes to happen. (Ewing,Greg; 2002)

Single threaded versus Multi threadedapplication

Continues…

Pages: 1 2

Twitter Digg Delicious Stumbleupon Technorati Facebook Email

No comments yet... Be the first to leave a reply!