Multithreading and Interactive Programs

Similar documents
Multithreading and Interactive Programs

Interactive Programming

CS 160: Interactive Programming

Last class: Today: Thread Background. Thread Systems

CS 261 Fall Mike Lam, Professor. Threads

Concurrency, Thread. Dongkun Shin, SKKU

CS 475. Process = Address space + one thread of control Concurrent program = multiple threads of control

Agenda Process Concept Process Scheduling Operations on Processes Interprocess Communication 3.2

CS 333 Introduction to Operating Systems. Class 3 Threads & Concurrency. Jonathan Walpole Computer Science Portland State University

Threads SPL/2010 SPL/20 1

CS 333 Introduction to Operating Systems. Class 3 Threads & Concurrency. Jonathan Walpole Computer Science Portland State University

CS 3305 Intro to Threads. Lecture 6

Introduction to pthreads

CS333 Intro to Operating Systems. Jonathan Walpole

EPL372 Lab Exercise 2: Threads and pthreads. Εργαστήριο 2. Πέτρος Παναγή

Threads. CS3026 Operating Systems Lecture 06

CS510 Operating System Foundations. Jonathan Walpole

CS510 Operating System Foundations. Jonathan Walpole

Threads. What is a thread? Motivation. Single and Multithreaded Processes. Benefits

Warm-up question (CS 261 review) What is the primary difference between processes and threads from a developer s perspective?

Outline. Threads. Single and Multithreaded Processes. Benefits of Threads. Eike Ritter 1. Modified: October 16, 2012

CS Lecture 3! Threads! George Mason University! Spring 2010!

Threads. studykorner.org

Chapter 4: Multi-Threaded Programming

JAVA CONCURRENCY FRAMEWORK. Kaushik Kanetkar

Chapter 4: Threads. Overview Multithreading Models Thread Libraries Threading Issues Operating System Examples Windows XP Threads Linux Threads

High Performance Computing Course Notes Shared Memory Parallel Programming

CSE 153 Design of Operating Systems Fall 2018

Operating Systems 2 nd semester 2016/2017. Chapter 4: Threads

LSN 13 Linux Concurrency Mechanisms

CSE 333 SECTION 9. Threads

Processes. 4: Threads. Problem needs > 1 independent sequential process? Example: Web Server. Last Modified: 9/17/2002 2:27:59 PM

CS533 Concepts of Operating Systems. Jonathan Walpole

Motivation. Threads. Multithreaded Server Architecture. Thread of execution. Chapter 4

Operating Systems. Operating System Structure. Lecture 2 Michael O Boyle

Chapter 4: Threads. Chapter 4: Threads

ECE 574 Cluster Computing Lecture 8

Parallel Programming Languages COMP360

Lecture 4: Threads; weaving control flow

Outline. CS4254 Computer Network Architecture and Programming. Introduction 2/4. Introduction 1/4. Dr. Ayman A. Abdel-Hamid.

Multithreaded Programming

Chapter 3: Processes. Operating System Concepts 8th Edition

High Performance Computing Lecture 21. Matthew Jacob Indian Institute of Science

Concurrent Server Design Multiple- vs. Single-Thread

Exercise (could be a quiz) Solution. Concurrent Programming. Roadmap. Tevfik Koşar. CSE 421/521 - Operating Systems Fall Lecture - IV Threads

Chapter 5: Processes & Process Concept. Objectives. Process Concept Process Scheduling Operations on Processes. Communication in Client-Server Systems

Introduction to PThreads and Basic Synchronization

Processes. Johan Montelius KTH

THREADS. Jo, Heeseung

ECE 598 Advanced Operating Systems Lecture 23

Chapter 4: Threads. Operating System Concepts 9 th Edition

Computer Systems Laboratory Sungkyunkwan University

A process. the stack

CS370 Operating Systems

CS 5523 Operating Systems: Midterm II - reivew Instructor: Dr. Tongping Liu Department Computer Science The University of Texas at San Antonio

CS 326: Operating Systems. Process Execution. Lecture 5

Multitasking. Embedded Systems

CS420: Operating Systems

Chapter 4: Threads. Operating System Concepts. Silberschatz, Galvin and Gagne

CHAPTER 3 - PROCESS CONCEPT

Concurrent Programming

4. Concurrency via Threads

CSE 4/521 Introduction to Operating Systems

Actors in the Small. Making Actors more Useful. Bill La

I.-C. Lin, Assistant Professor. Textbook: Operating System Concepts 8ed CHAPTER 4: MULTITHREADED PROGRAMMING

Chapter 4: Threads. Operating System Concepts 9 th Edit9on

Chapter 4: Multithreaded Programming

Chapter 4: Threads. Operating System Concepts 9 th Edition

Questions from last time

Parallel Programming: Background Information

Parallel Programming: Background Information

Concurrent Programming

Start of Lecture on January 22, 2014

OPERATING SYSTEM. Chapter 4: Threads

!! How is a thread different from a process? !! Why are threads useful? !! How can POSIX threads be useful?

Threaded Programming. Lecture 9: Alternatives to OpenMP

Processes and Threads

Operating System Architecture. CS3026 Operating Systems Lecture 03

Process Characteristics

What is concurrency? Concurrency. What is parallelism? concurrency vs parallelism. Concurrency: (the illusion of) happening at the same time.

! How is a thread different from a process? ! Why are threads useful? ! How can POSIX threads be useful?

Concurrency. Johan Montelius KTH

Concurrency and Threads

Processes and Threads

Executive Summary. It is important for a Java Programmer to understand the power and limitations of concurrent programming in Java using threads.

Concurrent Programming

CSE 120 Principles of Operating Systems

Grand Central Dispatch

CSE 153 Design of Operating Systems

CSE 153 Design of Operating Systems Fall 2018

CS 153 Lab4 and 5. Kishore Kumar Pusukuri. Kishore Kumar Pusukuri CS 153 Lab4 and 5

Concurrency Control. Synchronization. Brief Preview of Scheduling. Motivating Example. Motivating Example (Cont d) Interleaved Schedules

CSE 120 Principles of Operating Systems

Warm-up question (CS 261 review) What is the primary difference between processes and threads from a developer s perspective?

Concurrent & Distributed Systems Supervision Exercises

Computation Abstractions. Processes vs. Threads. So, What Is a Thread? CMSC 433 Programming Language Technologies and Paradigms Spring 2007

CSC 4320 Test 1 Spring 2017

Process Characteristics. Threads Chapter 4. Process Characteristics. Multithreading vs. Single threading

Threads Chapter 4. Reading: 4.1,4.4, 4.5

CS 31: Introduction to Computer Systems : Threads & Synchronization April 16-18, 2019

Transcription:

Multithreading and Interactive Programs CS160: User Interfaces John Canny.

Last time Model-View-Controller Break up a component into Model of the data supporting the App View determining the look of the application Controller for mediating and event management Provides scalability and extensibility Damage-Repair A strategy for fast rendering

This time Multithreading for interactivity need and risks Some design patterns for multithreaded programs Debugging multithreaded programs

Why Multithreading? Interactive programs need to respond fast to user input. Direct manipulation assumes that objects onscreen move with the user s hand.

Why Multithreading? But not all code returns from event-handling so fast: File access Network operations Database lookup Simulation

Why Multithreading? We at least need to decouple the code processing screen events from the code that handles them. But we often need to do more to make sure the code runs robustly, even in the presence of errors.

Why Multithreading? Other processes that need to stay live Getting help Aborting (need to handle the abort operation) Doing something else while waiting for a long operation Running these modules in a single thread violates several usability heuristics. Which are they?

Why Multithreading? More examples: Multicore processors: use all the CPUs Dynamic widgets: Clocks Progress indicators Mail inbox PS your course projects don t have to be multithreaded, they are interactive prototypes.

What is a Thread? A thread is a partial virtual machine. Each thread has its own stack, but shares most of memory, i.e. heap space and program memory* with other threads. for (i=0; i<n; i++) { tmp = A[i]; A[i] = B[i]; B[i] = tmp; } VM1 VM2 * Threads may also have some private heap space, called Thread-Local Storage (TLS).

What is a Process? A process is a complete virtual machine with its own stack and heap. Since processes don t share memory with each other, they need other mechanisms (e.g. system message queues) to communicate with each other.

Processes vs. Threads i.e. processes are like dating but multithreaded programs are like living together.

Why Threads? Because they share memory, threads support very efficient and versatile communication. In fact, such communication is basically free, and any data structure can be used. Well, even if communication is free, code development certainly isn t. Multithreaded programming is probably the biggest productivity killer of all time.

Threading Hell Semaphores Locks Join() Mutexes P() Monitors V() Signal() Spawn() Condition Var Wait()

Why is it hard? Threading primitives are lower-level than high-level language constructs. Not standardized across OSes. Nondeterminism. Combinatorics:

Combinatorics Consider 10 steps of a simple non-branching program. A = B; C = 4*A; Y = exp(c); Z = 5*R; There is exactly one way that these statements can execute in a single-threaded program.

Combinatorics But if two threads execute the same 10 statements, how many possible orderings of operations are there? A = B; C = 4*A; Y = exp(c); Z = 5*R; 20 A: 184756 = - but really more since there will be 10 >> 10 instructions in the executable

Thread Safety Code is thread safe if it can be called from multiple threads without interaction between them. A simple C++ function or method works just fine: int fact (int n) { int i, p; for (i=1,p=1; i<=n; i++) p*=i; } return p; Separate ints i,p are created on the stack each time the function is called. Each thread has its own copy.

Thread un-safety But what happens if we change the variables to static (shared or global)? int fact (int n) { static int i, p; for (i=1,p=1; i<=n; i++) p*=i; } return p;

Thread Safety and OOP Object-Oriented Programming uses class instances, and these are usually heap-allocated. Heap storage is shared, and so class instances can be seen and manipulated by all threads. You need to take extra care when writing OOP multi-threaded code for this reason. Arbitrary sharing of state in objects is very likely to lead to chaos (Windows Vista Explorer crash) Try to use a well-structured concurrent design pattern, like the ones we are going to describe.

Threading Systems Posix threads (Pthreads) are available on many systems including gcc and Mac OSX. They support: pthread_create: create a thread with a function to run pthread_join: wait for a thread to finish (from outside) pthread_exit: finish a thread from inside (optional)

Thread Coordination Data primitives to allow one thread to process data without interference from others. Include semaphores, mutexes, condition variables, monitors. These are all low-level constructs and very error-prone. Objective-C, Java and C# support a synchronized block wrapper.

Objective-C @synchronized The following code snippet protects a segment of code from access by more than one thread: - (void) setx:(double)x0 { @synchronized (self) { x = x0; } } The first thread to execute this obtains a lock (usually a Posix mutex) on the class instance (self). Another thread will block at the @synchronized statement until the first thread exits this block.

Objective-C @synchronized - (void) setx:(double)x0 { @synchronized (self) { x = x0; } } // Reads or writes wait on self if a read/write is in progress - (double) x { double tmp; @synchronized (self) { tmp = x; } return tmp; }

Objective-C @synchronized @synchronized (someobject) { code; } // Expands as @try { lock(someobject); // (implementation-dependent). code; // Exceptions or return might happen here. } @finally { // But this happens anyway. unlock(someobject); }

Objective-C @synchronized @synchronized is usually applied to class instances (self), but can also be used to lock a class variable, which locks all methods for that class. The @synchronized block does the right thing most of the time, and is implementation independent. Similar constructs exist in Java and C#.

Design Patterns Delegate Message queue / Dataflow Database / Model-View-Controller Actor

Delegate Problem: a message is to be executed by calling a class method, but asynchronously. E.g. the message may come from a queue. We want a persistent piece of data that includes the target (a class instance), the method to call, and possibly other arguments. Solution: Create a new class that stores a reference to the target class, and has a single member function such as invoke. To use the delegate, we send it an invoke message which causes it to call a specific method on its target class.

Delegate @interface drawer { } id mytarget; - (id) init:(id) t0; // Init the target class - (void) invoke; // Universal message to do something @end

Delegate @implementation drawer; - (id) init:(id)t0 { // Just create with target class if (self = [super init]) { mytarget = t0; } return self; } - (void) invoke { [mytarget draw]; } @end

Delegate Now we can drop such delegate objects into a queue. When they pop out, a message handler always calls invoke on them. The appropriate method on the appropriate target class gets called. They can also be used to implement widget connections ala InterfaceBuilder.

Message Queues Two threads (or processes) with limited communication can use a message queue. This is a simple implementation which minimizes coordination and data-sharing between the two threads. Thread 1 sendmsg(msg); Queue Thread 2 while (1) { msg=getevent(); process(msg); }

Message Queues Examples:.NET MessageQueue objects Thread-specific message queues in the Windows GUI API Java Message Service (JMS) And similar primitives exist between processes: POSIX message queues (in Linux) CORBA asynchronous messaging

Message Queues We can realize a message queue by synchronizing queue and dequeue operations in a standard queue. i.e. @Implementation msgqueue; - (void) enqueue:(id)arg0 { @synchronized (self) { } } // enqueue arg0

Message Queues We can realize a message queue by synchronizing queue and dequeue operations in a standard queue. i.e. - (id) dequeue { id tmp; @synchronized (self) { } return tmp; } // dequeue tmp

Message Queues Advantages: Code is basically sequential in each thread. Much easier to develop and debug. Reusable queue libraries do all the hard work. Disadvantages: Inefficient if too much communication, or if complex data structures are passed. Need message loop/dispatcher on receiving end not very modular.

Model-View-Controller MVC is an excellent pattern for concurrent programming: State is centralized in the model, no other communication needed Controllers+Viewers run independently, and each can have its own thread. View View View Model Controller Controller Controller

Model-View-Controller Databases provide an excellent backend for the model: Transactions complex updates are atomic. Locking at different scales: an entire table or a row of a table. Consistency constraints (relations). Publish-Subscribe Triggers View View View Model Controller Controller Controller

MVC for multithreading Advantages: Extensible, modular. Easy to develop and debug. Save much coding if a database is used. Disadvantages: Heavy use of resources (space, time, memory). Discourages quick information flows. Can be very slow with many users if locks are too coarse.

Actor An actor is a class instance that runs its own thread. Since data and methods are closely associated in a class, using a single thread to run the actor is very modular. The actor will need an event loop to process incoming events. Synchronized queues or mailboxes support communication. Actor 1 while (1) { msg=getevent(); process(msg); }

Actor Advantages: Easy to design like the sequential version of the class, but with the event loop added. Good alignment between threads and data, minimizes contention and probability of inconsistency. Exploits multicore processors. Disadvantages: A system of actors can be very complex to model. Best to use a mixture of actors and passive classes. A large multi-actor system is resource-intensive (memory, time, )

Debugging Not too difficult similar to sequential debugging with a few extra operations: Attach: attach the debugger to a running process. List threads: list the running threads in the program. Select a thread: Pick one to view or step through. Thread-specific breakpoints: Stop the program when one specific thread reaches a program line.

Debugging Note: The debugger normally runs all threads but checks when certain conditions (breakpoints or steps) are met by a particular thread. So debugged execution is very similar to live execution, except for the pauses.

e.g. GDB > attach pid > i th (info threads) > th 3 > b MyFile.m:10 thread 3 > c (continue) > s (step) > ptype classname > p varname > p [self mthd] > p self->x > set var x=3 > M-p (meta-p) attach to a process by its id show all the running threads select thread 3 stop program when thrd 3 reaches here start running single-step this thread show a class definition print a variable value call a class method show instance variable value set a variable to a value recall last command

Example The sample program has connections from Button StopWatch ProgressBar Button and ProgressBar are widgets, StopWatch is an invisible actor. Button 1 down on StopWatch 1 up off time setv ProgressBar 1

Review Multithreading for interactivity need and risks Design patterns for multithreaded programs Delegate Message queue MVC Actor Debugging multithreaded programs