Why Threads Are A Bad Idea (for most purposes)

Slides:



Advertisements
Similar presentations
CS 443 Advanced OS Fabián E. Bustamante, Spring 2005 Resource Containers: A new Facility for Resource Management in Server Systems G. Banga, P. Druschel,
Advertisements

Threads, Events, and Scheduling Andy Wang COP 5611 Advanced Operating Systems.
Chapter 4: Threads. Overview Multithreading Models Threading Issues Pthreads Windows XP Threads.
Precept 3 COS 461. Concurrency is Useful Multi Processor/Core Multiple Inputs Don’t wait on slow devices.
W4118 Operating Systems OS Overview Junfeng Yang.
Threads 1 CS502 Spring 2006 Threads CS-502 Spring 2006.
Based on Silberschatz, Galvin and Gagne  2009 Threads Definition and motivation Multithreading Models Threading Issues Examples.
Concurrency, Threads, and Events Robbert van Renesse.
CPS110: Implementing threads/locks on a uni-processor Landon Cox.
A. Frank - P. Weisberg Operating Systems Introduction to Tasks/Threads.
Threads CNS What is a thread?  an independent unit of execution within a process  a "lightweight process"  an independent unit of execution within.
CS 3013 & CS 502 Summer 2006 Threads1 CS-3013 & CS-502 Summer 2006.
Why Threads Are A Bad Idea (for most purposes) John Ousterhout Sun Microsystems Laboratories
Slide 3-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 3 Operating System Organization.
Operating Systems CSE 411 CPU Management Sept Lecture 11 Instructor: Bhuvan Urgaonkar.
Introduction and Overview Questions answered in this lecture: What is an operating system? How have operating systems evolved? Why study operating systems?
Process Management. Processes Process Concept Process Scheduling Operations on Processes Interprocess Communication Examples of IPC Systems Communication.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Thread Scheduling.
Background: Operating Systems Brad Karp UCL Computer Science CS GZ03 / M th November, 2008.
Chapter 4 – Threads (Pgs 153 – 174). Threads  A "Basic Unit of CPU Utilization"  A technique that assists in performing parallel computation by setting.
Lecture 5: Threads process as a unit of scheduling and a unit of resource allocation processes vs. threads what to program with threads why use threads.
Operating Systems CSE 411 CPU Management Sept Lecture 10 Instructor: Bhuvan Urgaonkar.
Department of Computer Science and Software Engineering
CSE 60641: Operating Systems Next topic: CPU (Process/threads/scheduling, synchronization and deadlocks) –Why threads are a bad idea (for most purposes).
Threads versus Events CSE451 Andrew Whitaker. This Class Threads vs. events is an ongoing debate  So, neat-and-tidy answers aren’t necessarily available.
CS533 - Concepts of Operating Systems 1 Threads, Events, and Reactive Objects - Alan West.
1 Why Threads are a Bad Idea (for most purposes) based on a presentation by John Ousterhout Sun Microsystems Laboratories Threads!
CSCI/CMPE 4334 Operating Systems Review: Exam 1 1.
10/2/20161 Operating Systems Design (CS 423) Elsa L Gunter 2112 SC, UIUC Based on slides by Roy Campbell, Sam King,
Introduction to Operating Systems Concepts
Introduction to threads
Chapter 4: Threads.
Processes and threads.
Advanced Operating Systems CIS 720
Threads vs. Events SEDA – An Event Model 5204 – Operating Systems.
Why Events Are A Bad Idea (for high-concurrency servers)
Advanced Topics in Concurrency and Reactive Programming: Asynchronous Programming Majeed Kassis.
Processes and Threads Processes and their scheduling
Scheduler activations
Day 12 Threads.
CS399 New Beginnings Jonathan Walpole.
Chapter 2 Processes and Threads Today 2.1 Processes 2.2 Threads
Process Management Presented By Aditya Gupta Assistant Professor
Introduction to Operating System (OS)
Threads, Events, and Scheduling
Capriccio – A Thread Model
Chapter 4: Threads.
Chapter 26 Concurrency and Thread
CSE 451: Operating Systems Spring 2012 Module 6 Review of Processes, Kernel Threads, User-Level Threads Ed Lazowska 570 Allen.
Thread Implementation Issues
Threads and Concurrency
Threads Chapter 4.
Background and Motivation
Threads, Events, and Scheduling
Threads and Concurrency
Chapter 13: I/O Systems I/O Hardware Application I/O Interface
Operating Systems: A Modern Perspective, Chapter 3
Chapter 4: Threads.
Threads, Events, and Scheduling
Processes David Ferry CSCI 3500 – Operating Systems
Why Threads Are A Bad Idea (for most purposes)
Threads vs. Processes Hank Levy 1.
CS510 Operating System Foundations
Why Events Are a Bad Idea (for high concurrency servers)
Foundations and Definitions
Why Threads Are A Bad Idea (for most purposes)
CS703 – Advanced Operating Systems
CSE 153 Design of Operating Systems Winter 2019
CS 5204 Operating Systems Lecture 5
CMSC 202 Threads.
Presentation transcript:

Why Threads Are A Bad Idea (for most purposes) John Ousterhout Sun Microsystems Laboratories john.ousterhout@eng.sun.com http://www.sunlabs.com/~ouster

Introduction Threads: Grew up in OS world (processes). Evolved into user-level tool. Proposed as solution for a variety of problems. Every programmer should be a threads programmer? Problem: threads are very hard to program. Alternative: events. Claims: For most purposes proposed for threads, events are better. Threads should be used only when true CPU concurrency is needed.

What Are Threads? Shared state (memory, files, etc.) Threads General-purpose solution for managing concurrency. Multiple independent execution streams. Shared state. Pre-emptive scheduling. Synchronization (e.g. locks, conditions).

What Are Threads Used For? Operating systems: one kernel thread for each user process. Scientific applications: one thread per CPU (solve problems more quickly). Distributed systems: process requests concurrently (overlap I/Os). GUIs: Threads correspond to user actions; can service display during long-running computations. Multimedia, animations.

What's Wrong With Threads? casual wizards all programmers Visual Basic programmers C programmers C++ programmers Threads programmers Too hard for most programmers to use. Even for experts, development is painful.

Why Threads Are Hard Synchronization: Must coordinate access to shared data with locks. Forget a lock? Corrupted data. Deadlock: Circular dependencies among locks. Each process waits for some other process: system hangs. thread 1 lock A lock B thread 2

Why Threads Are Hard, cont'd Hard to debug: data dependencies, timing dependencies. Threads break abstraction: can't design modules independently. Callbacks don't work with locks. Module A Module B T1 T2 sleep wakeup deadlock! T1 calls Module A deadlock! Module B callbacks T2

Why Threads Are Hard, cont'd Achieving good performance is hard: Simple locking (e.g. monitors) yields low concurrency. Fine-grain locking increases complexity, reduces performance in normal case. OSes limit performance (scheduling, context switches). Threads not well supported: Hard to port threaded code (PCs? Macs?). Standard libraries not thread-safe. Kernel calls, window systems not multi-threaded. Few debugging tools (LockLint, debuggers?). Often don't want concurrency anyway (e.g. window events).

Event-Driven Programming One execution stream: no CPU concurrency. Register interest in events (callbacks). Event loop waits for events, invokes handlers. No preemption of event handlers. Handlers generally short-lived. Event Loop Event Handlers

What Are Events Used For? Mostly GUIs: One handler for each event (press button, invoke menu entry, etc.). Handler implements behavior (undo, delete file, etc.). Distributed systems: One handler for each source of input (socket, etc.). Handler processes incoming request, sends response. Event-driven I/O for I/O overlap.

Problems With Events Long-running handlers make application non-responsive. Fork off subprocesses for long-running things (e.g. multimedia), use events to find out when done. Break up handlers (e.g. event-driven I/O). Periodically call event loop in handler (reentrancy adds complexity). Can't maintain local state across events (handler must return). No CPU concurrency (not suitable for scientific apps). Event-driven I/O not always well supported (e.g. poor write buffering).

Events vs. Threads Events avoid concurrency as much as possible, threads embrace: Easy to get started with events: no concurrency, no preemption, no synchronization, no deadlock. Use complicated techniques only for unusual cases. With threads, even the simplest application faces the full complexity. Debugging easier with events: Timing dependencies only related to events, not to internal scheduling. Problems easier to track down: slow response to button vs. corrupted memory.

Events vs. Threads, cont'd Events faster than threads on single CPU: No locking overheads. No context switching. Events more portable than threads. Threads provide true concurrency: Can have long-running stateful handlers without freezes. Scalable performance on multiple CPUs.

Should You Abandon Threads? No: important for high-end servers (e.g. databases). But, avoid threads wherever possible: Use events, not threads, for GUIs, distributed systems, low-end servers. Only use threads where true CPU concurrency is needed. Where threads needed, isolate usage in threaded application kernel: keep most of code single-threaded. Event-Driven Handlers Threaded Kernel

Conclusions Concurrency is fundamentally hard; avoid whenever possible. Threads more powerful than events, but power is rarely needed. Threads much harder to program than events; for experts only. Use events as primary development tool (both GUIs and distributed systems). Use threads only for performance-critical kernels.