You are on page 1of 4

Parallel Programming in C#

In this article, we will discuss Threads,Asynchronus and


Parrallel programming. This is a key concept to improve
performance.
What is Parallel Programming?
o Synchronus Programming
o Asynchronus Programming
What is a Thread?
What is Multithreading?
Advantages of Multithreading
Different methods for Multithreading
Summary

What is Parallel Programming?


Parallel programming is a programming technique wherein the execution flow of the
application is broken up into pieces that will be done at the same time (concurrently) by
multiple cores, processors, or computers for the sake of better performance.
Before discussing Parallel programming, lets understand 2 important concepts.

Synchronous programming

Asynchronus programming

In Synchronus execution, the program runs all tasks in sequence.

That means when each task is triggered then the program waits unit it finishes before
starting the next one.
In Asynchronus execution, the program doesnt run all tasks in sequence.
That means it fires the tasks concurrently, and then waits for their end
So in simple words, When you execute something synchronously, you wait for it to finish
before moving on to another task. When you execute something asynchronously, you can
move on to another task before it finishes.
So now, the question is, If asynchronous execution takes less total time to finish than
synchronous execution, why would anybody choose synchronous execution?

Lets understand the advantages and disadvantages of both


programming modes.
In Synchronus mode, every task executes in sequence, so its easier to program. Thats the
way weve been doing it for years.
With asynchronous execution, you have few challenges:

You must synchronize tasks. for e.g. you run a task that must be executed after the
other three have finished. You will have to create a mechanism to wait for all tasks to
finish before launching the new task.

You must address concurrency issues. If you have a shared resource, like a list that
is written in one task and read in another, make sure that its kept in a known state.

There is no logical sequence anymore. The tasks can end at any time, and you dont
have control of which one finishes first.

But in synchronous programming we have below disadvantages:

It takes longer to finish.

It may stop the user interface (UI) thread. Typically, these programs have only one UI
thread, and when you use it as a blocking operation, you get the spinning wheel (and
not responding in the caption title) in your programnot the best experience for
your users.

It doesnt use the multicore architecture of the new processors. Regardless of


whether your program is running on a 1-core or a 64-core processor, it will run as
quickly (or slowly) on both.

Asynchronous programming eliminates these disadvantages: it wont hang the UI thread


(because it can run as a background task), and it can use all the cores in your machine and
make better use of machine resources. So, do you choose easier programming or better
use of resources? Fortunately, you dont have to make this decision. Microsoft has created
several ways to minimize the difficulties of programming for asynchronous execution.

What is a Thread?
A Thread is the smallest unit of code to which an operating system allocates CPU time.
In multithreading, a single process has multiple threads of execution.If the system has
multiple cpus then it can run in parallel.

Advantages of Multithreading or
Asynchronous Programming:
Lets look at below examples to understand it better.
1.You have a program that checks dozen websites to get pricing information for a product.
So in this case, if the program does everything in single thread:

Then the main program is blocked until the web search finishes.

The user interface is stuck and the user cant do anything

But if you run the web interface and search in different threads then the program can remain
responsive even while the search is still running.
2. Multithreading can also simplify your code. for e.g. you have a program that periodically
checks a collection of websites for news and stock prices. You can write a program that
repeatedly loops through each of the site to check them one after another and that would be
complicated.Another approach would be to assign separate thread to each website and let
each thread run independently.Now each thread will focus only on one website.

Different methods for Multithreading:


The .net framework provides several methods for multithreading.

PLINQ

Background worker
o This component executes code on a separate thread.It uses events to
communicate with main user interface thread.

Task Parallel Library (TPL)


o These tools let you easily run multiple methods in different threads or run
multiple instances of the same method with different parameters.

Tasks
o The task class let you create and run threads.

Threads
o The Thread class gives you lower level access to threads.

You might also like