Data structures for parallel programming in .NET
When working with parallel programming in .NET, and with PLINQ, you should take advantage of the data structures, types, and primitives that .NET provides. In this section, we will touch on concurrent collections and synchronization primitives.
Concurrent collections
Concurrent collections are useful when working with parallel programming. We will cover them in great detail in Chapter 9, but let’s quickly discuss how we can leverage them when working with PLINQ queries.
If you are simply selecting and sorting data with PLINQ, it is not necessary to incur the overhead that is added with the collections in the System.Collections.Concurrent
namespace. However, if you are calling a method with ForAll
that modifies items in your source data, you should use one of these current collections, such as BlockingCollection<T>
, ConcurrentBag<T>
, or ConcurrentDictionary<TKey, TValue>
. They can also guard against any...