Lecture18-multithreading-intro - Announcements Project 4...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
1 CS 3214 Computer Systems Godmar Back Lecture 18 Announcements •P ro jec t 4 due Nov 2 • Stay tuned for exercise 7 • Midterm returned today CS 3214 Fall 2011 2 11/4/2011 MULTI-THREADING 11/4/2011 CS 3214 Fall 2011 3 Processes vs Threads • Processes execute concurrently and share resources: – Files on disk; Open files (via inherited file descriptors); Terminal, etc. – Kernel-level concurrency P1 P3 P2 Kernel • but do not (usually) share any of their memory – cannot share data easily by exchanging pointers to it • Threads are separate logical flows of control (with separate stacks!) that share memory and can refer to same data – Different models and variations exist – Application-level concurrency 11/4/2011 CS 3214 Fall 2011 4 Cooperative Multi-threading • Special case of user-level threading – Is easy to implement using ‘swapcontext’ – see next slide • Support multiple logical execution flows – Each needs own stack so has its own (procedure-) local (automatic) variables – But share address space so shares heap, global vars (all kinds: global, global static, local static) • In cooperative multi-threading, a context switch can occur only if a thread voluntarily offers the CPU to (any) other thread (“yield”); later resumes and returns – Can build resource abstractions on top where threads yield if they cannot obtain the abstracted resource • This is called a “non-preemptive” model • If yield is directed (“yield to x”) this model is called “co- routines” 11/4/2011 CS 3214 Fall 2011 5 Cooperative Multithreading via ‘swapcontext’ static char stack[2][65536]; // a stack for each coroutine static ucontext_t coroutine_state[2]; // container to remember context // switch current coroutine (0 -> 1 -> 0 -> 1 . ..) static inline void yield_to_next(void) { static int current = 0; static void 11/4/2011 CS 3214 Fall 2011 6 int prev = current; int next = 1 - current; current = next; swapcontext(&coroutine_state[prev], &coroutine_state[next]); }
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 12/31/2011 for the course CS 3214 taught by Professor Staff during the Fall '11 term at Virginia Tech.

Page1 / 3

Lecture18-multithreading-intro - Announcements Project 4...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online