Friday, 19 September 2025

Synchronization techniques in .NET

 

 

A race condition occurs when two or more threads are able to access shared data and they try to change it at the same time. Because the thread scheduling algorithm can swap between threads at any point, we cannot know the order at which the threads will attempt to access the shared data. Therefore, the result of the change in data is dependent on the thread scheduling algorithm, i.e. both threads are 'racing' to access/change the data. The reason for having more race conditions among threads than processes is that threads can share their memory while a process can not.

For example Let's say we have two threads, T1 and T2, where both are accessing a shared variable x.  They first read data from the variable and then try to write data on the variable simultaneously.  They would race to find which of these threads can write the value last to the variable.  In this case, the last written value would be saved.

 As another example, we can have these two threads, T1 and T2, trying opposite operations on this variable.  Let thread T1 increase the value of the variable and the thread T2 decrease the value of the variable at the same point of time.  What is the result?  The value remains unchanged.

 

 

 

 

lock    : Ensures just one thread can access a resource, or section of code.

 
Mutex : Ensures just one thread can access a resource, or section of code. Can be used to prevent multiple instances of an application from starting.

 
Semaphore :Ensures not more than a specified number of threads can access a resource, or section of code.  

Wednesday, 26 February 2025

How to get data from DB

 https://ivanayael.medium.com/how-to-retrieve-information-from-the-db-with-asp-net-core-and-ef-and-create-a-web-api-2a47658ba3df