Data structures for concurrency 1
Ordinary collections are not thread safe Namespaces System.Collections System.Collections.Generics Classes List, LinkedList, Dictionary, HashSet, Queue, Stack etc. We need some thread safe collections for parallel programming Data structures for concurrency2
Collections Ordinary collectionsSimilar thread safe collections List, ordered collectionnone ConcurrentBag, not an ordered collection Stack ConcurrentStack Queue ConcurrentQueue Dictionary ConcurrentDictionary Data structures for concurrency3
Thread safe collections Less locking, means more concurrency Collections should only acquire the lock when they really need to. Namespace System.Collection.Concurrent IProducerConsumerCollection interface ConcurrentBag No order. Most efficient if the same thread does add and take ConcurrentQueue ConcurrentStack ConcurrentDictionary Data structures for concurrency4
Non-blocking thread safe collections Non-blocking The thread safe collections do not block a thread that tries to take an element from empty collection. Unlike BoundedBuffer from the Producer-Consumer exercise IProducerConsumerCollection methods Bool TryTake(out T item) Removes and item (if any) Returns TWO values Bool indicated whether and item was ready or not Item contains the element (if any) Bool TryPeek(out T item) Returns, but does not remove an item (if any) Data structures for concurrency5
BlockingCollection BoundedBuffer and UnboundedBuffer from the Producer-Consumer exercise are blocking A thread that tries to take an element from an empty collection will be blocked (Monitor.wait(…)) If the buffer is bounded a thread will be blocked on a full buffer System.Collections.Concurrent.BlockingCollection does the job of both BoundedBuffer and UnboundedBuffer Some constructors BlockingCollection() UnboundedBuffer, using a queue (FIFO) BlockingCollection(int capacity) BoundedBuffer, using a queue (FIFO) BlockingCollection(IProducerConsumerCollection coll) UnboundedBuffer, using coll to hold the data BlockingCollection(IProducerConsumerCollection coll, int capacity) BoundedBuffer, using coll to hold the data Data structures for concurrency6
BlockingCollection Methods Element Take() May block if collection is empty Bool TryTake(out element) Does not block Bool indicates whether and element was taken Void Add(element) May block if collection is bounded and full Bool TryAdd(element) Does not block Bool indicates where and element was added Properties BoundedCapacity, read-only Return the capacity, or Int.MaxValue if unbounded Count, read-only Returns the number of elements currently in the buffer Example: BlockingCollectionTrying Data structures for concurrency7
References and further readings Joseph Albahari: Threading in C# Part 5 Parallel Programming Concurrent collections MSDN System.Collections.Concurrent Namespace us/library/system.collections.concurrent(v=vs.110).aspx us/library/system.collections.concurrent(v=vs.110).aspx Gaston C. Hillar Professional Parallel Programming with C#, Master Parallel Extensions with.NET 4, Wrox/Wiley 2011 Chapter 4: Concurrent Collections, page Data structures for concurrency8