Thread Synchronization in Multithreading

Kailash Chandra Behera | Tuesday, June 14, 2022

Introduction

In a multithreaded application, it is required to coordinate with the running threading to avoid deadlock conditions. Here in this blog, we will learn what is synchronization or thread synchronization in C#.

Getting Started

In the multithreaded application when two or more threads are running, there is a chance that multiple threads may try to access to same resource at the same time. For example, if you have two threads, one that reads a text file and other threads writes to the text file, in this case both threads will try to access the text file at the same time. The first thread will try to read data from the text file, at the same time the second thread will try to write. This will lead to never ended task or deadlock.

Deadlock is a situation when a thread is waiting for an object lock, that is acquired by another thread and second thread is waiting for an object lock that is acquired by first thread. Since, both threads are waiting for each other to release the lock, the condition is called deadlock. Deadlock is common thing; you can say it is a part of multithreading.

To avoid this deadlock condition in multithreading C# enables you to coordinate the action of multiple threads by using synchronized methods or synchronized statements.

Synchronization is a technique which provides thread safety, thread safety means if two or more threads need to access a shared resource then that resource is used by only one thread at a time and other thread which need this resource must wait until the current thread completed its action.

How the Thread Synchronization Works

Thread synchronization is working based on the concept of monitoring. A monitor is an object that is used as lock to the data member and methods of a class. If any object or resource associates with monitor, then it ensures that only one thread has access to the resource at any given time.

An object is said to have entered the monitor when a thread acquires a lock. When a synchronized method starts execution, the resource or object is locked until the method completes its task and automatically when method completes the task. During the execution of synchronized method, the object is locked so that no other synchronized method ca be invoked.

Thread synchronization can be achieved using the following synchronization statements or methods.

  1. Lock Statement

    The Lock statement ensures that when an object or method of a class inters in to monitor state by a thread then no other thread acquire the object until the current thread releases the object. The object automatically will be released when the thread execution completes.

  2. Monitor

    Monitor work as like lock but it provides more control over the synchronization of various threads trying to access the same lock of code. For example when lock is applied by a thread to a object then the lock on object will be released automatically when thread execution completes but in the case of monitor manually lock need to be released when lock on object is no longer required.

  3. Mutex

    Mutex also providing the synchronized access of resource in a multithreaded environment but different is all the above synchronized method (Lock and Monitor) works in a process only, mean the scop of above two methos are inside a single process but Mutex provides synchronization access of resources over multiple process.

    For example, if you have two applications and you want synchronization access of a common object by these two applications then you can use mutex.

  4. Semaphore

    Semaphore is signalling mechanism and semaphore class works like the Monitor and Mutex class but lets you set a limit on how many threads have access to a critical section. It's often described as a nightclub (the semaphore) where the visitors (threads) stands in a queue outside the nightclub waiting for someone to leave in order to gain entrance. 

  5. Join

    In thread synchronization, join is a blocking mechanism that pauses the calling thread. This is done till the thread whose join method was called has completed its execution.

Thanks


No comments: