From the fact that the value has to go into the cpu

Info icon This preview shows pages 654–656. Sign up to view the full content.

View Full Document Right Arrow Icon
from the fact that the value has to go into the CPU so that it can calculate the new value, and then it has to be written back out again so that the new value is not forgotten. There will always be a read and then a write. Consider what happens when two threads try to increment the same Count field. Let’s call them Thread A and Thread B. Table 16-1 shows one possible sequence of events. In this case it works out fine: Count starts at 0, is incremented twice, and ends up at 2. Table 16-1. Two increments, one after the other Count Thread A Thread B 0 Read Count (0) 0 Add 1 (0 + 1 = 1) 1 Write Count (1) 1 Read Count (1) 1 Add 1 (1 + 1 = 2) 2 Write Count (2) But it might not work so well. Table 16-2 shows what happens if the work overlaps, as could easily happen with multiple logical processors. Thread B reads the current Count while Thread A was already part of the way through the job of incrementing it. When Thread B comes to write back its update, it has no way of knowing that Thread A has updated the value since B did its read, so it effectively loses A’s increment. Table 16-2. Lost increment due to overlap Count Thread A Thread B 0 Read Count (0) 0 Add 1 (0 + 1 = 1) Read Count (0) 1 Write Count (1) Add 1 (0 + 1 = 1) 1 Write Count 630 | Chapter 16: Threads and Asynchronous Code
Image of page 654

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
There are lots of variations on the order, some of which work fine and some of which fail. If your code makes possible an ordering that produces wrong results, sooner or later you’ll run into it. Don’t fall into the trap of believing that a highly improbable outcome is effectively impossible. You’ll be fooling yourself—sooner or later the problem will bite. The only difference with the highly improbable prob- lems is that they’re extremely hard to diagnose and debug. The example shown here is about as simple as a race gets. With real code things tend to be a lot more complex, as you will probably be dealing with data structures more intricate than a single integer. But in general, if you have information which is visible to multiple threads and at least one of those threads is changing that information in any way, race conditions are likely to emerge if you don’t take steps to prevent them. The solution to races is, on the face of it, obvious: the threads need to take it in turns. If threads A and B simply coordinated their operations so that either would wait until the other was done when an update is in progress, we could avoid the problem. Inter locked.Increment does exactly that, although it’s rather specialized. For the occasions when you’re doing something more complex than incrementing a field, .NET provides a set of synchronization mechanisms that let you force threads to take it in turns. We’ll get to these shortly. However, this solution introduces another class of problem.
Image of page 655
Image of page 656
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern