Chapter 5. Concurrent Collections

In this chapter, we are going to cover the following recipes:

  • Adding and removing items to BlockingCollection
  • Iterating a BlockingCollection with GetConsumingEnumerable
  • Performing LIFO operations with ConcurrentStack
  • Thread safe data lookups with ConcurrentDictionary
  • Cancelling an operation in a concurrent collection
  • Working with multiple producers and consumers
  • Creating object pool with ConcurrentStack
  • Adding blocking and bounding with IProducerConsumerCollection
  • Using multiple concurrent collections to create a pipeline

Introduction

Although System.Collections namespace offers a wide range of collections; the only thing which limits our use of them in a multi-threaded or parallel environment is that they are not thread safe. A non thread safe collection could lead to race conditions, which is a condition that occurs when two or more threads can access shared data and try to change it at the same time, producing unexpected errors.

Concurrent collections in .NET Framework 4.5 allow the developers to create type safe as well as thread safe collections. These collection classes form an essential part of the parallel programming feature and are available under the namespace System.Collections.Concurrent.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.111.1