Data structures for concurrency 1. Ordinary collections are not thread safe Namespaces System.Collections System.Collections.Generics Classes List, LinkedList,

Slides:



Advertisements
Similar presentations
Queues Printer queues Several jobs submitted to printer Jobs form a queue Jobs processed in same order as they were received.
Advertisements

Operating Systems Semaphores II
1 Linked lists Sections 3.2, 3.3, 3.5 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors.
– R 7 :: 1 – 0024 Spring 2010 Parallel Programming 0024 Recitation Week 7 Spring Semester 2010.
Queues A waiting line that grows by adding elements to its end and shrinks by taking elements from its front Line at the grocery store Cars in traffic.
CS252: Systems Programming Ninghui Li Program Interview Questions.
COSC 1P03 Data Structures and Abstraction 9.1 The Queue Whenever you are asked if you can do a job, tell 'em, "Certainly, I can!" Then get busy and find.
Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
Data Structure (Part I) Stacks and Queues. Introduction to Stack An stack is a ordered list in which insertion and deletions are made at one end. –The.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
COP3538 – Data Structures Using OOP Chapter 4 – Stacks and Queues.
1 Condition Synchronization. 2 Synchronization Now that you have seen locks, is that all there is? No, but what is the “right” way to build a parallel.
Threading Part 4 CS221 – 4/27/09. The Final Date: 5/7 Time: 6pm Duration: 1hr 50mins Location: EPS 103 Bring: 1 sheet of paper, filled both sides with.
Threading Wrapup CS221 – 4/29/09. Concurrent Collections Java supplies a set of concurrent collections you can use manage sets of data in a multi- threaded.
Enforcing Mutual Exclusion, Semaphores. Four different approaches Hardware support Disable interrupts Special instructions Software-defined approaches.
Main Index Contents 11 Main Index Contents Model for a Queue Model for a Queue The Queue The Queue ADTQueue ADT (3 slides) Queue ADT Radix Sort Radix Sort.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation - FIFO As items are added they are chronologically ordered, items are removed.
1 Queues and Priority Queues Chapter 8. 2 Introduction to Queues A queue is a waiting line – seen in daily life –Real world examples – toll booths, bank,
1 Data Structures  We can now explore some advanced techniques for organizing and managing information  Chapter 12 of the book focuses on: dynamic structures.
1/30/2004CSCI 315 Operating Systems Design1 Processes Notice: The slides for this lecture have been largely based on those accompanying the textbook Operating.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation - FIFO As items are added they are chronologically ordered, items are removed.
C# Programming: From Problem Analysis to Program Design1 Advanced Collections C# Programming: From Problem Analysis to Program Design 3 rd Edition 8.
30-Jun-15 Stacks. What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything.
Stacks  Standard operations: IsEmpty … return true iff stack is empty Top … return top element of stack Push … add an element to the top of the stack.
1/26/2007CSCI 315 Operating Systems Design1 Processes Notice: The slides for this lecture have been largely based on those accompanying the textbook Operating.
Stacks, Queues, and Deques
Stacks, Queues, and Deques. A stack is a last in, first out (LIFO) data structure –Items are removed from a stack in the reverse order from the way they.
Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 8 Ming Li Department of.
Stacks, Queues, and Deques
שירן חליבה Concurrent Queues. Outline: Some definitions 3 queue implementations : A Bounded Partial Queue An Unbounded Total Queue An Unbounded Lock-Free.
Collection types Collection types.
Chapter 3: Processes. 3.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 3: Processes Process Concept Process Scheduling Operations.
Task Parallel Library (TPL)
1 Linked Stack Chapter 4. 2 Linked Stack We can implement a stack as a linked list. Same operations. No fixed maximum size. Stack can grow indefinitely.
6.3 Peterson’s Solution The two processes share two variables: Int turn; Boolean flag[2] The variable turn indicates whose turn it is to enter the critical.
1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
Evolution of.NET Collections (C#) Chen Dong
Data Structures (part 2). Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that last ‘rush’
Data Structures Using C++
3.1 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9 th Edition Interprocess Communication Processes within a system may be.
4061 Session 21 (4/3). Today Thread Synchronization –Condition Variables –Monitors –Read-Write Locks.
Processes. Chapter 3: Processes Process Concept Process Scheduling Operations on Processes Cooperating Processes Interprocess Communication Communication.
CS212: OPERATING SYSTEM Lecture 2: Process 1. Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 3: Process-Concept.
Advanced Concurrency Topics Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Gal Milman Based on Chapter 10 (Concurrent Queues and the ABA Problem) in The Art of Multiprocessor Programming by Herlihy and Shavit Seminar 2 (236802)
Kruse/Ryba ch031 Object Oriented Data Structures Queues Implementations of Queues Circular Implementation of Queues.
Processes. Process Concept Process Scheduling Operations on Processes Interprocess Communication Communication in Client-Server Systems.
Generics & Collection Classes Version 1.0. Topics Generic Methods and Classes Generic Collection Classes List Enumerators Queue Stack LinkedList.
6.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Module 6: Process Synchronization.
Chapter 4 ADTs Stack and Queue. 4-2 Formal ADT Specifications The Java interface construct lets us collect together method interfaces into a syntactic.
Chapter  Array-like data structures  ArrayList  Queue  Stack  Hashtable  SortedList  Offer programming convenience for specific access.
CSC 360, Instructor: Kui Wu IPC. CSC 360, Instructor: Kui Wu Agenda 1.The need to communicate 2.Shared memory 3.Message passing.
Chapter 6 Synchronization Dr. Yingwu Zhu. The Problem with Concurrent Execution Concurrent processes (& threads) often access shared data and resources.
Stack: a Linked Implementation
CS505 Data Structures and Algorithms
Classical Synchronization Problems
Critical sections, locking, monitors, etc.
THURSDAY, OCTOBER 17 IN LAB
Condition Variables and Producer/Consumer
COMPUTER 2430 Object Oriented Programming and Data Structures I
CSC 143 Queues [Chapter 7].
Condition Variables and Producer/Consumer
Chapter 30 Condition Variables
Another Means Of Thread Synchronization
Patterns for Programming Shared Memory
Fundaments of Game Design
Patterns for Programming Shared Memory
Stacks.
Don Porter Portions courtesy Emmett Witchel
Chengyu Sun California State University, Los Angeles
Presentation transcript:

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