Threading in C#

What is Process?

Process is nothing but executing program. Process has a unique process ID. We can view the process within which program is being executed using windows task manager.

Here we can see different programs running under different process.

Another example is calculator program. When we start calculator program, calculator process will start.

What is Threading?

Thread is light weight process. A single process can have multiple threads.

Threads are the smallest unit of execution scheduled by the operating system.
Any program or application has a minimum of one thread be it web application or a console window application.

There are 2 types of threads:

  • Foreground Thread
  • Background Thread

Main purpose of threading is do parallel code execution. By default foreground thread is created. Even if main application quits, then also foreground thread will continue to execute.

In case of background thread,when main application quits, background thread quits.

Namespace: System.Threading

Purpose: To make Application responsive.

How to debug Thread?

  • Name the Threads
  • When we are debugging 1 thread another thread is paused/halt
  • If we want to debug 1 thread and pause another thread using freeze option and start it using Thaw option.
  • Use break point option- conditional debugging.

Thread Safe:

If object is behaving abnormally in multi threaded environment, it is not thread safe.

Example: Divide by number program in loop where we set Num1 = 0 and Num2 = 0 after Num1/Num2.
Here  DividebyZero exception is raised.

So user needs to ensure that only 1 thread is accessing the critical section.

Different types of locks:

  • Lock(Monitor)
  • Mutex
  • Semaphore

Mutex:

  • Mutex works across multiple programs/Process.
  • We can have global mutex.
  • Mutex are objects that are owned by owned by thread at a time. Other threads are forced to wait until first thread releases it.

Example

There is 1 room having 1 key to it.Now 3 persons want to use it. Person who has key can access the room.

Similarly Mutex is object owned by thread so there is ownership in mutex. Mutex allow 1 thread to access the resource.

Semaphore:

  • A Semaphore restricts the number of simultaneous users of a shared resource up to a maximum number.
  • Thread can request access to the resource(decrementing the semaphore) & can signal that they have finished using the resource(incremeting the semaphore)
  • Semaphore is signalling mechanism. It allows a number of thread to access shared resources.

Example:

A  semaphore is like nightclub, it has certain capacity enforced by bouncer. Once it is full no more people can enter & a queue is build up outside. Then for each perosn that leaves, one person enters from the head of the queue.

TPL: Task parallel Library

Problems around Threading:

We want to run the logic parallely and it should use the CPU power to maximum. If my machine is 2 core processor then it should run half the iterations on 1 processor and another half on second processor. However that doesn’t happen and everything runs on 1 processor.

This can be verified using perfmon tool.

Single processor ->
Thread 1 and Thread 2 – It switches time between threads. it is called time slicing or context switching.

Two Processors-> Expectation is Processor 1 should execute Thread 1 and Processor 2 should execute Thread 2.
Processor 1 -> Thread 1
Processor 2 -> Thread 2

Namespace: System.Threading.Task

Purpose: To utilize CPU power to maximum. TPL encapsulates multi-core execution from end user.

How TPL does thread pooling?

Leave a Reply

Your email address will not be published. Required fields are marked *