Concurrency and Immutability

Slides:



Advertisements
Similar presentations
Operating Systems Part III: Process Management (Process Synchronization)
Advertisements

Global Environment Model. MUTUAL EXCLUSION PROBLEM The operations used by processes to access to common resources (critical sections) must be mutually.
ECE 454 Computer Systems Programming Parallel Architectures and Performance Implications (II) Ding Yuan ECE Dept., University of Toronto
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Process Synchronization. Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores.
Concurrency The need for speed. Why concurrency? Moore’s law: 1. The number of components on a chip doubles about every 18 months 2. The speed of computation.
PARALLEL PROGRAMMING with TRANSACTIONAL MEMORY Pratibha Kona.
PZ11B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ11B - Parallel execution Programming Language Design.
Concurrent Processes Lecture 5. Introduction Modern operating systems can handle more than one process at a time System scheduler manages processes and.
Chapter 11: Distributed Processing Parallel programming Principles of parallel programming languages Concurrent execution –Programming constructs –Guarded.
1 Concurrency: Deadlock and Starvation Chapter 6.
29-Jun-15 Java Concurrency. Definitions Parallel processes—two or more Threads are running simultaneously, on different cores (processors), in the same.
A. Frank - P. Weisberg Operating Systems Introduction to Cooperating Processes.
Simple Wait-Free Snapshots for Real-Time Systems with Sporadic Tasks Håkan Sundell Philippas Tsigas.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
1 Program5 Due Friday, March Prog4 user_thread... amount = … invoke delegate transact (amount)... mainThread... Total + = amount … user_thread...
Concurrency Control 1 Fall 2014 CS7020: Game Design and Development.
Software Transactional Memory Should Not Be Obstruction-Free Robert Ennals Presented by Abdulai Sei.
Thread basics. A computer process Every time a program is executed a process is created It is managed via a data structure that keeps all things memory.
1 Parallel execution Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
NETW 3005 Monitors and Deadlocks. Reading For this lecture, you should have read Chapter 7. NETW3005 (Operating Systems) Lecture 06 - Deadlocks2.
Adding Concurrency to a Programming Language Peter A. Buhr and Glen Ditchfield USENIX C++ Technical Conference, Portland, Oregon, U. S. A., August 1992.
Tutorial 2: Homework 1 and Project 1
Multithreading / Concurrency
Background on the need for Synchronization
G.Anuradha Reference: William Stallings
CS240: Advanced Programming Concepts
Atomic Operations in Hardware
Atomic Operations in Hardware
Computer Engg, IIT(BHU)
Threads, Concurrency, and Parallelism
Lecture 5: GPU Compute Architecture
143a discussion session week 3
Lecture 5: GPU Compute Architecture for the last time
Clojure 6 Concurrency 20-Nov-18.
Semaphore and Multithreading
Parallelism Can we make it faster? 29-Nov-18.
COP 4600 Operating Systems Fall 2010
Shared Memory Programming
Thread Synchronization
Parallel execution Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Lecture: Coherence and Synchronization
Background and Motivation
Dr. Mustafa Cem Kasapbaşı
Java Concurrency 17-Jan-19.
Implementing Mutual Exclusion
Concurrency: Mutual Exclusion and Process Synchronization
Software Transactional Memory Should Not be Obstruction-Free
CSCI1600: Embedded and Real Time Software
Clojure 4 Concurrency 24-Feb-19.
Implementing Mutual Exclusion
Clojure 4 Concurrency 13-Apr-19.
- When you approach operating system concepts there might be several confusing terms that may look similar but in fact refer to different concepts:  multiprogramming, multiprocessing, multitasking,
Java Concurrency.
CSE 153 Design of Operating Systems Winter 19
Parallelism Can we make it faster? 27-Apr-19.
CS333 Intro to Operating Systems
Java Concurrency.
Threads and Multithreading
CSCI1600: Embedded and Real Time Software
Lecture 23: Transactional Memory
Chapter 3: Processes Process Concept Process Scheduling
Java Concurrency 29-May-19.
Parallel execution Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Software Engineering and Architecture
CSE 542: Operating Systems
Concurrency in GO CS240 23/8/2017.
Chapter 3: Process Management
Synchronization and liveness
Presentation transcript:

Concurrency and Immutability

Concurrency vs. Parallelism Concurrency is being able to juggle multiple tasks at the same time Parallelism is begin able to execute multiple tasks simultaneously

Concurrency vs. Parallelism Concurrency has been an issue for several decades (even on single-core machines). Slow operations can run in the background I/O Expensive computations

Concurrency vs. Parallelism Now it's common to have multiple cores on laptops and phones Making full use of the hardware requires splitting your task amongst those cores.

Concurrency vs. Parallelism Despite their differences, the programming issues of concurrency and parallelism are fairly similar.

Mutual Exclusion Main Problem: Contention for shared resources

Mutual Exclusion Main Problem: Contention for shared resources Solution: Mutual exclusion Locks Semaphores Etc.

Clojure's Way: Concurrency primitives Future Promises Channels

Future A future provides a way of referencing a specific result that is (presumably) being computed in parallel (future (println "starting computation") (Thread/sleep 5000) (println "finished computation") 42)

Future The value computed by a future can be accessed by calling deref The thread will block until the value becomes available You can put a limit on the amount of time to wait And a default value to return if it times out You can also check for availability with (realized? <future variable>)

Promise Similar to a future It defines a placeholder for a value to be computed later The difference is that any thread could potentially deliver a value to the promise Once a value has been delivered all future deliver calls are ignored.

Atom A holder for values that may change. The values are still immutable but one thread can change an atom's value and any future reads from that atom (even by another thread) will reflect the new value.

swap! The exclamation-point ending is a longstanding LISP convention to indicate an impure function The official rule in Clojure is that it should be used for any function that isn't legal in an STM* transaction *more about this later

swap! Provides a safe way to make a change to an atom Even if other threads may be modifying the atom It takes an atom as its first argument and an update function as its second argument The update function takes the old value and uses it to compute the new value This is particularly useful for adding/removing elements to/from collections

reset! Used when you don't care about the previous value. Takes an atom as its first argument and the new value as its second argument.

STM - Software Transactional Memory swap! can only be used to update a single atom, but sometimes you need to modify several atoms simultaneously

STM For example, if there were three containers Source Dest1 Dest2 And two threads taking elements from Source the first places elements into Dest1 the second places elements into Dest2 We want to be sure that each element ends up in either Dest1 or Dest2, but not both

STM If we are quite unlucky, the same element could be read from Source by both thread1 and thread2 before the other modified Source to indicate what it had taken.

STM: ref References are like atoms, but they can be used in STM transactions They are modified with alter which is just like swap! but is safe for use in STM transactions hence the lack of exclamation point

STM: dosync STM transactions take place within the body of a dosync statement. If any of the references read in a dosync block are modified, the dosync block re- executes This can lead to starvation if there is a slow dosync block contending with a number of fast ones.

Core.async Inspired by the 'go' language Which was inspired by CSP Communicating Sequential Processes Define channels that carry data between processes