programming c# 4.0.pdf

Class is a better choice than manualresete vent if

Info icon This preview shows pages 673–675. Sign up to view the full content.

class is a better choice than ManualResetE vent if you’re using .NET 4 or later. Synchronization Primitives | 649
Image of page 673

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

Countdown .NET 4 introduces the CountdownEvent class, which provides a handy solution to a fairly common problem: knowing when you’re done. Remember back in Example 16-6 , we ran into an issue with the thread pool. We queued up a couple of pieces of work, but we had no way of knowing when they were done. One solution to that would be to use the Task Parallel Library, which we’ll get to shortly, but an alternative would have been to use the CountdownEvent class. CountdownEvent is very simple. For each piece of work you start, you call AddCount (or if you know how many pieces of work there will be up front, you can pass that number into the constructor). For each piece of work that completes you call Signal . And if you need to wait for all outstanding work to complete (e.g., before your program exits), just call Wait . BlockingCollection The System.Collections.Concurrent namespace provides various collection classes that are designed to be used in multithreaded environments. They look a little different from the normal collection classes because they are designed to be used without needing any locking, which means they can’t offer features that rely on things staying consistent from one moment to the next. Numerical indexing is out, for example, because the number of items in the collection may change, as we saw when trying to use List<T> in a multithreaded fashion in Example 16-11 . So these are not thread-safe versions of normal collection classes—they are collections whose APIs are designed to support multithreaded use without the caller needing to use locks. BlockingCollection is not just a multithreaded collection; it also offers associated co- ordination. It provides a way for threads to sit and wait for items to become available in the collection. Its Take method will block if the collection is empty. Once data be- comes available, Take will return one item. Any number of threads may be waiting inside Take at any time, and other threads are free to call Add . If you Add enough items that all the threads waiting to Take are satisfied, and then you keep calling Add , that’s when items start to get added to the collection. And if the collection is nonempty, calls to Take will return immediately. This allows you to have one or more threads dedicated to processing work items generated by other threads. The BlockingCollection acts as a kind of buffer—if you generate items faster than you process them, they will queue up in the BlockingCollec tion , and if the processing threads catch up, they will block efficiently until more items come along. You could use this in a WPF application that needs to do slow work in the background—the UI thread could add work into a blocking collection, and then one or more worker threads could take items from the collection and process them. This is not hugely different from using the thread pool, but it gives you the opportunity to limit 650 | Chapter 16: Threads and Asynchronous Code
Image of page 674
Image of page 675
This is the end of the preview. Sign up to access the rest of the document.
  • Spring '15

{[ 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