Download presentation
Presentation is loading. Please wait.
Published byHannah Lee Modified over 9 years ago
1
Data structures for concurrency 1
2
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
3
Collections Ordinary collectionsSimilar thread safe collections List, ordered collectionnone ConcurrentBag, not an ordered collection Stack ConcurrentStack Queue ConcurrentQueue Dictionary ConcurrentDictionary Data structures for concurrency3
4
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
5
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
6
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
7
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
8
References and further readings Joseph Albahari: Threading in C# Part 5 Parallel Programming Concurrent collections http://www.albahari.com/threading/part5.aspx#_Concurrent_Collections http://www.albahari.com/threading/part5.aspx#_Concurrent_Collections MSDN System.Collections.Concurrent Namespace http://msdn.microsoft.com/en- us/library/system.collections.concurrent(v=vs.110).aspx http://msdn.microsoft.com/en- 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 103 - 156 Data structures for concurrency8
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.