Presentation is loading. Please wait.

Presentation is loading. Please wait.

Why Threads Are A Bad Idea (for most purposes) John Ousterhout Sun Microsystems Laboratories

Similar presentations


Presentation on theme: "Why Threads Are A Bad Idea (for most purposes) John Ousterhout Sun Microsystems Laboratories"— Presentation transcript:

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

2 Why Threads Are A Bad Idea September 28, 1995, slide 2 Introduction u 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? u Problem: threads are very hard to program. u Alternative: events. u Claims: –For most purposes proposed for threads, events are better. –Threads should be used only when true CPU concurrency is needed.

3 Why Threads Are A Bad Idea September 28, 1995, slide 3 What Are Threads? u General-purpose solution for managing concurrency. u Multiple independent execution streams. u Shared state. u Pre-emptive scheduling. u Synchronization (e.g. locks, conditions). Shared state (memory, files, etc.) Threads

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

5 Why Threads Are A Bad Idea September 28, 1995, slide 5 What's Wrong With Threads? u Too hard for most programmers to use. u Even for experts, development is painful. casualwizards all programmers Visual Basic programmers C programmers C++ programmers Threads programmers

6 Why Threads Are A Bad Idea September 28, 1995, slide 6 Why Threads Are Hard u Synchronization: –Must coordinate access to shared data with locks. –Forget a lock? Corrupted data. u Deadlock: –Circular dependencies among locks. –Each process waits for some other process: system hangs. lock Alock B thread 1thread 2

7 Why Threads Are A Bad Idea September 28, 1995, slide 7 Why Threads Are Hard, cont'd u Hard to debug: data dependencies, timing dependencies. u Threads break abstraction: can't design modules independently. u Callbacks don't work with locks. Module A Module B T1T2 sleepwakeup deadlock! Module A Module B T1 T2 deadlock! callbacks calls

8 Why Threads Are A Bad Idea September 28, 1995, slide 8 Why Threads Are Hard, cont'd u 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). u 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?). u Often don't want concurrency anyway (e.g. window events).

9 Why Threads Are A Bad Idea September 28, 1995, slide 9 Event-Driven Programming u One execution stream: no CPU concurrency. u Register interest in events (callbacks). u Event loop waits for events, invokes handlers. u No preemption of event handlers. u Handlers generally short-lived. Event Loop Event Handlers

10 Why Threads Are A Bad Idea September 28, 1995, slide 10 What Are Events Used For? u Mostly GUIs: –One handler for each event (press button, invoke menu entry, etc.). –Handler implements behavior (undo, delete file, etc.). u 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.

11 Why Threads Are A Bad Idea September 28, 1995, slide 11 Problems With Events u 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). u Can't maintain local state across events (handler must return). u No CPU concurrency (not suitable for scientific apps). u Event-driven I/O not always well supported (e.g. poor write buffering).

12 Why Threads Are A Bad Idea September 28, 1995, slide 12 Events vs. Threads u 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. u 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.

13 Why Threads Are A Bad Idea September 28, 1995, slide 13 Events vs. Threads, cont'd u Events faster than threads on single CPU: –No locking overheads. –No context switching. u Events more portable than threads. u Threads provide true concurrency: –Can have long-running stateful handlers without freezes. –Scalable performance on multiple CPUs.

14 Why Threads Are A Bad Idea September 28, 1995, slide 14 Should You Abandon Threads? u No: important for high-end servers (e.g. databases). u 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. Threaded Kernel Event-Driven Handlers

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

16 Why Threads Are A Bad Idea September 28, 1995, slide 16 Discussions u Slide 8 says "Threads not well supported:"These days POSIX Threads library (Pthreads) is available even on Windows! Does this weaken the Presenter argument against threads? u I however disagree with Slide 15's point "Threads more powerful than events, but power is rarely needed". Cores are increasing. GHz/Core will stabilize/decrease. To get the same performance in future we will need threads! Daily Desktop applications like encoding, rendering can always use more power (more cores and more threads). Comments?

17 Why Threads Are A Bad Idea September 28, 1995, slide 17 Discussions u This paper was written in 1995. Today, 14 years later, with the coming of CPU concurrency even in low-end consumer machines, is the paper still relevant, or should we be looking to use threads everywhere we can? u If a malicious user tries to intercept the event, then the system will be totally out of control. Am I right? u suppose there are several processors this time. When each event loop runs at the respective processor, and some of them use the same resources, then, the synchronized problems are still there. Am I right?

18 Why Threads Are A Bad Idea September 28, 1995, slide 18 Discussions u The paper claims that concurrency is fundamentally hard and should be avoided whenever possible. Do we buy this? u How does event mechanism operate in order to avoid using a lock? How does it handle the situation that, for example, two events occur at the same time, both of which want to access the same address space?


Download ppt "Why Threads Are A Bad Idea (for most purposes) John Ousterhout Sun Microsystems Laboratories"

Similar presentations


Ads by Google