Wednesday, June 11, 2014

Multithreading

Introduction
The term “multithread programming” may sound complicated, but it is quite easy to do in C#.net. This article explains how multithreading works on your typical, general-purpose computer. You will learn how the operating system manages thread execution and how to manipulate the Thread class in your program to create and start managed threads. This article also renders the information you need to know when programming application with multiple threads such as Thread class, pools, threading issues and backgroundWorker.
Multithreading Overview
A thread is an independent stream of instructions in a program. Each written program is a paradigm of sequential programming in which they have a beginning, an end, a sequence, and a single point of execution. A thread is similar to sequential program. However, a thread itself is not a program, it can’t run on its own, but runs within a program.
The real concern surrounding threads is not about a single sequential thread, but rather the use of multiple threads in a single program all running at the same time and performing different tasks. This mechanism referred as Multithreading. A thread is considered to be a lightweight process because it runs within the context of a program and takes advantage of resources allocated for that program.
Threads are important both for client and server applications. While in C# program coding, when you type something in editor, the dynamic help (intellisense) Windows immediately shows the relevant topics that fit to the code. One thread is waiting for input from the user, while other does some background processing. A third thread can store the written data in a temporary file, while another one downloads a file from a specific location.
With the task manager, you can turn on the Thread column and see the processes and the number of threads for every process. Here, you can notice that only cmd.exe is running inside a single thread while all other applications use multiple threads.
The operating system schedules threads. A thread has priority and every thread has its own stack, but the memory for the program code and heap are shared among all threads of a single process.
A process consists of one or more threads of execution which is simply referred as threads. A process always consists of at least one thread called as Main thread (Main() method). A single thread process contains only one thread while multithread process can contains more than one thread of execution.
On a computer, the operating system loads and starts applications. Each application or service runs as a separate process on the machine. The following image illustrates that there are quite few processes actually running than there are applications. Many of the processes are background operating system processes that are started automatically when the computer powers up.
System.Threading Namespace
Under .NET platform, the System.Threading namespace provides a number of types that enable the direct construction of multithreaded application.
TypeDescription
ThreadIt represents a thread that execute within the CLR. Using this, we can produce additional threads in application domain.
MutexIt is used for synchronization between application domains.
MonitorIt implements synchronization of objects using Locks and Wait.
SmaphoreIt allows limiting the number of threads that can access a resource concurrently.
InterlockIt provides atomic operations for variables that are shared by multiple threads.
ThreadPoolIt allows you to interact with the CLR maintained thread pool.
ThreadPriorityThis represents the priority level such as High, Normal, Low.
System.Threading.Thread class
The Thread class allows you to create and manage the execution of managed threads in your program. They are called managed threads because you can directly manipulate each thread you create. You will found the Thread class along with useful stuffs in the System.Threading namespace.
MemberTypeDescription
CurrentThreadStaticReturn a reference of current running thread.
SleepStaticSuspend the current thread for a specific duration.
GetDoaminStaticReturn a reference of current application domain.
CurrentContextStaticReturn a reference of current context in which the thread currently running.
PriorityInstance levelGet or Set the Thread priority level.
IsAliveInstance levelGet the thread state in form of True or False value.
StartInstance levelInstruct the CLR to start the thread.
SuspendInstance levelSuspend the thread.
ResumeInstance levelResume a previously suspended thread.
AbortInstance levelInstruct the CLR to terminate the thread.
NameInstance levelAllows establishing a name to thread.
IsBackgroundInstance levelIndicate whether a thread is running in background or not.

No comments:

Post a Comment