Title: Thread Models
1Thread Models
- Preemptive threads
- Advantage Enforce fairness
- Cooperative threads
- Advantage Easy to understand
- Semi-cooperative threads
- Best of both worlds!
void thread_1(void) while (1)
list head NULL void thread_2(void) list
node mknode() node-gtnext head head
node
2Semi-Cooperative Threads
- How it works
- Multiprocessor support
- Compiler tells runtime which code segments cannot
be run concurrently - Threads are scheduled accordingly
- Forced yields ) fewer constraints
Compiler
Inserts guarded yield points at regular intervals
Loader
Verifies that yield points are placed
appropriately
Runtime
Preempts by setting flag program yields at next
point
vs.
3Applications
- Case 1 CCured
- Transformation can introduce bugs
- Case 2 Javas StringBuffer
int SEQ p1 int SEQ p2 p1 p2
seqp_int p1 seqp_int p2 p1._p p2._p p1._b
p2._b p1._e p2._e
public synchronized StringBuffer append(char ch)
ensureCapacity(count1) valuecount
ch return this
4 Building Better Threads
- Long-term goal Make threaded programming
simple! - Exploit semi-cooperative approach
- Commit changes to shared state using simple
operations (e.g., pointer swap) - Use optimistic concurrency control
- E.g., ensureCapacity()
- Improve communication between compiler and
runtime system - Semi-cooperative threads
- Linked stacks
Key Principle The compiler can make simplifying
assumptions on a per-program basis if it tells
the runtime system about those assumptions!