.Net Interview Question: How is Shared Data Managed in Threading?

There are certain situations that you need to be careful with when using threads. If two threads (e.g. the main and any worker threads) try to access the same variable at the same time, you'll have a problem. This can be very difficult to debug because they may not always do it at exactly the same time. To avoid the problem, you can lock a variable before accessing it. However, if the two threads lock the same variable at the same time, you will have a deadlock problem. To avoid dead lock we need to use the ‘SyncLock’ keyword as shown in the code snippet below.

SyncLock x
'Do something with x
End SyncLock