Tuesday, July 31, 2018

Making Sense of Thread Synchronization in C#

A "thread race condition" is when two threads are trying to write to a shared resource simultaneously, and access to your shared resource has not been correctly synchronized. It is one of the most common problems that can occur when creating multi-threaded software and is notoriously difficult to debug since it might occur once every 10,000 times you run your program. Hence, using your debugger to sort out these types of problems in your code is like using a hammer and a saw to try to fix your car. To illustrate the problem, imagine the following rally taking place somewhere in an alternative universe, where thread synchronization is impossible, due to the nature of e=mc2+1...

The .NET framework contains lots of helper classes to make sure we have synchronized access to our shared resources, but the intricacies of using these classes, to make sure you are accessing shared resources (correctly) often makes your code much more verbose and difficult to understand. Littering your code with tons of "synchronization code" also makes it much less readable.



from DZone.com Feed https://ift.tt/2AqJOTZ

No comments:

Post a Comment