A Scheme concurrency library

Slides:



Advertisements
Similar presentations
CAS3SH3 Midterm Review. The midterm 50 min, Friday, Feb 27 th Materials through CPU scheduling closed book, closed note Types of questions: True & False,
Advertisements

Chap 4 Multithreaded Programming. Thread A thread is a basic unit of CPU utilization It comprises a thread ID, a program counter, a register set and a.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 4: Multithreaded Programming.
Threads. Objectives To introduce the notion of a thread — a fundamental unit of CPU utilization that forms the basis of multithreaded computer systems.
Chapter 5 Processes and Threads Copyright © 2008.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 5: Threads Overview Multithreading Models Threading Issues Pthreads Solaris.
Threads - Definition - Advantages using Threads - User and Kernel Threads - Multithreading Models - Java and Solaris Threads - Examples - Definition -
3.5 Interprocess Communication Many operating systems provide mechanisms for interprocess communication (IPC) –Processes must communicate with one another.
Server Architecture Models Operating Systems Hebrew University Spring 2004.
3.5 Interprocess Communication
1 School of Computing Science Simon Fraser University CMPT 300: Operating Systems I Ch 4: Threads Dr. Mohamed Hefeeda.
 2004 Deitel & Associates, Inc. All rights reserved. Chapter 4 – Thread Concepts Outline 4.1 Introduction 4.2Definition of Thread 4.3Motivation for Threads.
1 School of Computing Science Simon Fraser University CMPT 300: Operating Systems I Ch 4: Threads Dr. Mohamed Hefeeda.
Java 5 Threading CSE301 University of Sunderland Harry Erwin, PhD.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Objectives Thread definitions and relationship to process Multithreading.
Process Concept An operating system executes a variety of programs
Chapter 4: Threads READ 4.1 & 4.2 NOT RESPONSIBLE FOR 4.3 &
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 4: Threads Overview Multithreading Models Threading Issues.
Why The Grass May Not Be Greener On The Other Side: A Comparison of Locking vs. Transactional Memory Written by: Paul E. McKenney Jonathan Walpole Maged.
Multithreaded Web Server.
Chapter 4: Threads. 4.2CSCI 380 Operating Systems Chapter 4: Threads Overview Multithreading Models Threading Issues Pthreads Windows XP Threads Linux.
14.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Chapter 4: Threads.
Object Oriented Analysis & Design SDL Threads. Contents 2  Processes  Thread Concepts  Creating threads  Critical sections  Synchronizing threads.
Fast Multi-Threading on Shared Memory Multi-Processors Joseph Cordina B.Sc. Computer Science and Physics Year IV.
 2004 Deitel & Associates, Inc. All rights reserved. 1 Chapter 4 – Thread Concepts Outline 4.1 Introduction 4.2Definition of Thread 4.3Motivation for.
Practical OOP using Java Basis Faqueer Tanvir Ahmed, 08 Jan 2012.
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 13 Threads Read Ch 5.1.
CSE 451: Operating Systems Section 5 Midterm review.
Chapter 2 Processes and Threads Introduction 2.2 Processes A Process is the execution of a Program More specifically… – A process is a program.
Chapter 4 – Threads (Pgs 153 – 174). Threads  A "Basic Unit of CPU Utilization"  A technique that assists in performing parallel computation by setting.
1 CSCI 6900: Design, Implementation, and Verification of Concurrent Software Eileen Kraemer August 24 th, 2010 The University of Georgia.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 4: Threads Modified from the slides of the text book. TY, Sept 2010.
Multithreaded Programing. Outline Overview of threads Threads Multithreaded Models  Many-to-One  One-to-One  Many-to-Many Thread Libraries  Pthread.
1 OS Review Processes and Threads Chi Zhang
CSC 360, Instructor: Kui Wu Thread & PThread. CSC 360, Instructor: Kui Wu Agenda 1.What is thread? 2.User vs kernel threads 3.Thread models 4.Thread issues.
Threads. Readings r Silberschatz et al : Chapter 4.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 4: Threads.
E81 CSE 532S: Advanced Multi-Paradigm Software Development Chris Gill Department of Computer Science and Engineering Washington University in St. Louis.
Operating System Concepts
Lecturer 3: Processes multithreaded Operating System Concepts Process Concept Process Scheduling Operation on Processes Cooperating Processes Interprocess.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 4: Threads.
Chapter 4 – Thread Concepts
Chapter 4: Multithreaded Programming
Prepared by Oussama Jebbar
PROCESS MANAGEMENT IN MACH
Advanced Topics in Concurrency and Reactive Programming: Asynchronous Programming Majeed Kassis.
Processes and Threads Processes and their scheduling
Chapter 4 – Thread Concepts
Chapter 4: Multithreaded Programming
Chapter 4: Multithreaded Programming
CS533 Concepts of Operating Systems
Threads and Locks.
Chapter 4: Threads.
Chapter 4: Threads.
Chapter 4: Threads.
Modified by H. Schulzrinne 02/15/10 Chapter 4: Threads.
Chapter 4: Threads.
Race conditions and Synchronization
Dr. Mustafa Cem Kasapbaşı
Chapter 4: Threads.
Threads Chapter 5 2/17/2019 B.Ramamurthy.
Threads Chapter 5 2/23/2019 B.Ramamurthy.
Chapter 4: Threads & Concurrency
Chapter 4: Threads.
Chapter 4: Threads.
Chapter 4: Threads.
Chapter 4: Threads.
CS533 Concepts of Operating Systems Class 4
Synchronization and liveness
Presentation transcript:

A Scheme concurrency library Takashi Kato @tk_riple

But Scheme has only SRFI-18 Introduction Number of CPUs are not one Using all resources requires concurrency programming But Scheme has only SRFI-18

The SRFI defines multithreading APIs These are very primitive

Example (import (rnrs) (srfi :18)) (define results '()) (define lock (make-mutex)) (define (fib n) (if (< n 2) n (+ (fib (- n 1)) (fib (- n 2))))) (define (async-fib n) (thread-start! (make-thread (lambda () (let ((r (fib n))) (mutex-lock! lock) (set! results (cons r results)) (mutex-unlock! lock) #t))))) (map thread-join! (do ((i 0 (+ i 1)) (r '() (cons (async-fib 25) r))) ((= i 1000) r)))

Problems Using global lock *1 If it's locked by parent thread? Result values are globally stored *1 Creating 1000 threads *1 The example was deliberately written like that.

(util concurrent)*1 A library provides concurrent data structures and APIs Portable for both R6RS and R7RS Requires SRFI-18 support (and SRFI-99) Unfortunately, SRFI-18 isn‘t widely supported… repository: https://github.com/ktakashi/scheme-concurrent

Shared queue Queue with atomic operations Retrieving an element from empty queue would wait Get (2) Put (3) Block! Block! Shared Queue Element Element Block! Put (1) Get (4)

Using shared queue (import (rnrs) (srfi :18) (util concurrent)) (define results (make-shared-queue)) (define (fib n) (if (< n 2) n (+ (fib (- n 1)) (fib (- n 2))))) (define (async-fib n) (thread-start! (make-thread (lambda () (shared-queue-put! results (fib n)) #t)))) (define threads (do ((i 0 (+ i 1)) (r '() (cons (async-fib 25) r))) ((= i 1000) r))) (do ((i 0 (+ i 1))) ((= i 1000)) (shared-queue-get! results)) (map thread-join! threads)

Thread pool Thread creation might be expensive Resources are finite On Windows, 2000 thread would be the limit Reusing existing thread resolves the issues

Usual thread pool Queue Job Job Job Job Thread 1 Thread 2 Thread n

Our thread pool Queue Job Job Thread 1 Pool Queue Job Job Container Thread n

Thread pool (2) Each managed thread has own channel To avoid abondant mutex Thread pool check the least busy thread Managed threads tell their availability to the pool To make best case thread choosing O(1)

Using thread pool (import (rnrs) (srfi :18) (util concurrent)) (define results (make-shared-queue)) (define thread-pool (make-thread-pool 100)) (define (fib n) (if (< n 2) n (+ (fib (- n 1)) (fib (- n 2))))) (define (async-fib n) (lambda () (shared-queue-put! results (fib n)) #t)) (do ((i 0 (+ i 1))) ((= i 1000)) (thread-pool-push-task! thread-pool (async-fib 25))) (do ((i 0 (+ i 1))) ((= i 1000)) (shared-queue-get! results)) ;; Cannot retrieve execution result

Future and Executor Thread pool can't retrieve execution result Future is a task to be executed by executor Executor executes futures After the execution, users can retrieve the results from futures

Using future and executor (import (rnrs) (srfi :18) (util concurrent)) (define results (make-shared-queue)) (define executor (make-thread-pool-executor 100 push-future-handler)) (define (fib n) (if (< n 2) n (+ (fib (- n 1)) (fib (- n 2))))) (define (async-fib n) (lambda () (shared-queue-put! results (fib n)) #t)) (define futures (do ((i 0 (+ i 1)) (r '() (cons (executor-submit! executor (async-fib 25)) r))) ((= i 1000) r))) (do ((i 0 (+ i 1))) ((= i 1000)) (shared-queue-get! results)) (map future-get futures)

Conclusion Using the library reduces: Explicit locks Explicit thread creation Resource management This means: Less dead lock Better stability

Questions?