Introduction
Now a day’s multi-threading questions becomes most favorite question of interview, in this article we are going to discuss the important points that are required while working in multi-threading and this points will help you out to track interview.
Getting Started
Before starting reading this article I would like to suggest you go through about threading because this article is not covering anything about threading.
When we are working in system resources or the function that contains lengthy processes, at that time only one thing is coming in our mind that is the implementation of threading or implementation of multi-threading.
Threads are asynchronous in nature, this asynchronous allows time-consuming task to be performed in the background while the application windows and controls remain responsive.
The asynchronous nature of thread means access to resources such as file handles, network connections, and memory must be coordinated otherwise two or more threads could access the same resource at the same time without awareness of other’s actions.
But sometimes it is required to share resources with multiple threads to perform multi-tasking action, in this situation proper communication between threads is required. This is called thread synchronization. In my next article, we will discuss in detail about Thread synchronization.
However this thread synchronization can sometimes lead to deadlocks, race condition. To avoid this deadlock and race condition .Net Framework provided interlocked class, lock keyword, and Monitor class to perform actions safely in multi-threading.
This Interlocked class provides atomic operation for resources that are shared by multiple threads. It contains some methods that are helping to protect against errors which can occur when the scheduler switches contexts while a thread is updating a variable that can be accessed by other threads, or when two threads are executing concurrently on separate processors. The members of this class do not throw exceptions.
Lock ensures that the clock of code or resource consumption is completing without interruption by any other threads. However, there certain situations where the lock should not be used, don’t use a lock on the object that is in control by your application and don’t lock the public variable, because code beyond your control may lock on the object as well. This could create deadlock situations where two or more threads wait for the release of the same object.
Monitor class provides the same feature that the lock statement provides, but the difference is that but the monitor class provides more control over the synchronization of various threads trying to access the same lock of code.
In the above content, we saw that how resources are consumed by thread isolating, now we will see how threads are communicating(one thread while consuming resource tells other threads to wait till completion of this task) after successfully completing their tasks or synchronization is happened between threads.
.Net framework provides synchronization events and Mutex class for communication between threads. The synchronization events have two states signaled and un-signaled, that can be used to activate and suspend threads. There are two types of synchronization events one is AutoResetEvent and other is ManualResetEvent. The Mutex can be used for both isolated execution of block of code and thread synchronization.
Summary
Hope reading this article you must have get ideas about multi-threading, how resources can be consume isolated, how communication happens between threads.
Thanks