40 likes | 50 Views
This article discusses the advantages of enforcing fairness and easy understanding in multi-threaded programming using preemptive, cooperative, and semi-cooperative thread models. It explores how compiler-inserted guarded yield points and runtime preemption can enhance thread scheduling and improve program performance. Additionally, it proposes using optimistic concurrency control and improved communication between the compiler and runtime system to simplify threaded programming.
E N D
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!