When working with shared data in threading how do you implement synchronization ?

Q

.NET INTERVIEW QUESTIONS - When working with shared data in threading how do you implement synchronization ?

✍: Guest

A

There are certain situtations 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'll have a deadlock problem.
SyncLock x
'Do something with x
End SyncLock

2009-12-15, 4347👍, 0💬