Really do negate the benefitcontrived though this

Info icon This preview shows pages 653–655. Sign up to view the full content.

View Full Document Right Arrow Icon
really do negate the benefit—contrived though this example may be, much worse ex- amples have cropped up in real systems. Some implementations may come out worse on some systems and better on others. For example, some parallel algorithms take a considerable hit relative to their sequential counterparts in order to be able to scale well on more processors. Such an algorithm might make sense only on systems where you have a large number of processors—it might be slower than the single-threaded version on a dual-core system, but very worthwhile on a system with 16 logical processors, for example. The bottom line is that if you want to understand whether a parallel solution is effective, you need to compare it against a single-threaded solution on the same hardware. Just because your CPU loads indicate that you’re being extremely parallel, that’s not nec- essarily the same as being really fast. And unless your code will only ever run on a single hardware configuration, you need to perform the same comparison on lots of different machines to get a good idea of how often a parallel solution might be the best choice. Multithreaded Coding Is Hard Even when multithreaded code provides a demonstrable performance benefit, it’s very hard to get right. We’ve already seen a few bizarre behaviors in some extremely simple examples. Achieving correct behavior in a real concurrent system can be very chal- lenging. So we’ll look at two of the most common classes of pitfalls before examining some strategies for avoiding them. Race conditions The anomalies we’ve seen so far have all been examples of a kind of concurrency hazard known as a race , so called because the outcome is determined by which participant gets to a particular place first. Example 16-1 displays different output each time it runs, because there are three threads all trying to print to the same console window, and the only thing that determines which one gets to print the next line is whichever happens to be the next to call Console.WriteLine . There is no coordination between the threads, and so they all pile in at once. It’s a relatively complicated example in some ways, ‡ More accurately, it’s whichever thread acquires the lock Console.WriteLine uses internally to serialize access to the console. Threads | 629
Image of page 653

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

View Full Document Right Arrow Icon
because most of the race happens where we can’t see it—that example spends almost all of its time inside Console.WriteLine . It’s easier to understand races when you can see all of the code. So consider the broken variation of Example 16-8 where the concurrently executing Go method used a simple Count += 1 instead of Interlocked.Increment . We saw that using the += operator resulted in lost increments, but why? The += operator has to do three things: first it must read the current value of Count , then it has to add 1 to that value, and finally it has to store the result back into the variable. The RAM chips in your computer don’t have the ability to perform calculations, so there’s no getting away
Image of page 654
Image of page 655
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