• No results found

Why Threads Are A Bad Idea (for most purposes)

N/A
N/A
Protected

Academic year: 2021

Share "Why Threads Are A Bad Idea (for most purposes)"

Copied!
8
0
0

Loading.... (view fulltext now)

Full text

(1)

Why Threads Are A Bad Idea

(for most purposes)

John Ousterhout

Sun Microsystems Laboratories

[email protected] 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.

(2)

Why Threads Are A Bad Idea September 28, 1995, slide 3

What Are Threads?

υ General-purpose solution for managing concurrency. υ Multiple independent execution streams.

υ Shared state.

υ Pre-emptive scheduling.

υ Synchronization (e.g. locks, conditions).

Shared state (memory, files, etc.)

Threads

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.

(3)

Why Threads Are A Bad Idea September 28, 1995, slide 5

What's Wrong With Threads?

υ Too hard for most programmers to use. υ Even for experts, development is painful.

casual all programmers wizards

Visual Basic programmers

C programmers C++ programmers

Threads programmers

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.

lock A lock B

(4)

Why Threads Are A Bad Idea September 28, 1995, slide 7

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!

Module A Module B T1

T2

deadlock!

callbacks calls

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?).

(5)

Why Threads Are A Bad Idea September 28, 1995, slide 9

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.

(6)

Why Threads Are A Bad Idea September 28, 1995, slide 11

Problems With Events

υ Long-running handlersmake 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 stateacross 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.

(7)

Why Threads Are A Bad Idea September 28, 1995, slide 13

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. Threaded Kernel

(8)

Why Threads Are A Bad Idea September 28, 1995, slide 15

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).

References

Related documents

United Nations offices, Funds and Programmes and other subsidiary organs and organizations of the United Nations System may use the International Decade for People of African

Table 6: Policy on permanent settlement: By year and by income Table 7: Policy on temporary settlement: By year and by income Table 8: Policy on family reunification: By year and

1) The average payoff in the group is an empirically invalid reference standard for explaining punishment individual behaviour. Approaches that rely on this comparison standard

The Directorate General of Planning and Health Information System (DGPHIS) was created in the Ministry of Health to ensure coordination in terms of the overall

physician admission medication orders Place completed form into patient’s chart Medication reconciliation at admission complete Patient Transfer (Inhouse) Patient Discharge (To

It’s particularly important to include in the offer letter a list of confidentiality, non-compete, non-solicitation, invention assignment, and any other agreements that might

The close match between these q/S properties and the q/S properties of the water sampled by shipboard CTD stations encompassing eddy-a (Figure 8b) indicate that the eddy was

The NAP identifies a total of 14 areas for priority action. These are: 1) institutional arrangements 2)chieftaincy and chiefdom boundary disputes 3) promotion of awareness and