Thread Models
E N D
Presentation Transcript
Thread 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->next = head; head = node; }
Semi-Cooperative Threads Compiler Inserts guarded yield points at regular intervals • How it works: • Multiprocessor support • Compiler tells runtime which code segments cannot be run concurrently • Threads are scheduled accordingly • Forced yields ) fewer constraints Loader Verifies that yield points are placed appropriately Runtime Preempts by setting flag; program yields at next point vs.
Applications • Case 1: CCured • Transformation can introduce bugs • Case 2: Java’s 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(count+1); value[count++] = ch; return this; }
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!