i

ASP.Net A Complete Guide

Multithreading Issues

Multithreading:

Before understanding the concept of threading, we should first understand what is mean by a thread.Thread is an independent collection of instruction in a program. The thread cannot run on its own and need the program context. We can call thread as a lightweight process. Multithreading is the OS's ability to run multiple threads at the same time, and performing various tasks is referred to as Multithreading. With the help of the Task Manager window, you can see the processes and the number of threads for every process.

With the task manager, you can turn on the Thread and see the processes and the number of threads for every process. The operating system has the capacity schedules threads. A thread has its priority & every thread has its own stack, but note the memory for the program code and heap are shared between all threads of a single process.

Multithreading is useful to increase the performance as well as to reduce the complexity, but while working on this, it has introduced another issue, and those are as below:

  • Synchronization Challenges

  • Deadlocks

  • Race conditions

1. Synchronization Challenges:

Problem:

The problem you will encounter while working with the multithreaded programming is synchronizing access to a shared resource.

The main problem occurs when multiple threads share access to a single object, and both might potentially try to modify the object at the same time.

Solution:

The ReaderWriterLockSlim class allows us to specify if our code is writing to the object or simply reading from the object.

This enables multiple readers' entrance at the same time, but denies any write code access until all other read and write threads are done doing their stuff.

By encapsulating the ReaderWriterLockSlim into a simple class, all of a sudden, it solves the problem without repetitive code.

2. Deadlocks:

This situation occurs when each of the two threads tries to lock a resource that has already locked by others. Neither thread can able to proceed further.

There are many methods of the managed threading classes provide time-outs to help you to find deadlocks. Ex: The below code attempts to acquire a lock on the object named lock object. When the lock is not obtained in 300 milliseconds, Then Monitor.printer will return false.

if (Monitor.TryEnter(lock object, 300)) { 

    try { 

        // Place code protected by the Monitor here. 

    } 

    finally { 

        Monitor.Exit(lockObject); 

    } 

else { 

    // Code to execute if the times out attempts. 

3. Race conditions:

Race conditions occur when the output of a program depends on the more than 2-thread when they reach to the particular point of code block run first.

A simple example of race condition is incrementing a field. If a class has a private static field (Count)and if that is incrementing each time when a class instance is created. The static Value required to load from 'Count' to register to increment the value and to store it in count again. Suppose in the multithreaded application the same field that is 'Count' is accessing by the multiple threads, then if first thread resumes execution and stores its value, it overwrites 'count' without considering the fact that the value has changed already in between somewhere.

The above-mentioned race condition is easily avoided by using the Interlocked class method, such as Interlocked. Increment.