Lecture19-multithreading-locking - Announcements Project 5...

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

View Full Document Right Arrow Icon
1 CS 3214 Computer Systems Godmar Back Lecture 19 Announcements •P ro jec t 5 due Nov 16 CS 3214 Fall 2011 2 11/4/2011 MULTI-THREADING 11/4/2011 CS 3214 Fall 2011 3 Accessing global variables /* Define a mutex and initialize it. */ static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; static int counter = 0; /* A global variable to protect. */ /* Function executed by each thread. */ static void * 11/4/2011 CS 3214 Fall 2011 4 static void increment(void *_) { int i; for (i = 0; i < 1000000; i++) { pthread_mutex_lock(&lock); counter++; pthread_mutex_unlock(&lock); } } movl counter, %eax incl %eax movl %eax, counter Anatomy of a Race Condition Thread 1 movl counter , %eax Thread 2 movl counter , %eax incl %eax IRQ ± OS decides to context switch time 0 0 11/4/2011 CS 3214 Fall 2011 5 incl %eax movl , counter incl %eax movl %eax , counter %eax – Thread 1’s copy %eax – Thread 2’s copy counter – global variable, shared ± IRQ ± 0 1 1 1 1 Final result: counter is 1, should be 2 Assume counter == 0 initially Race Conditions • Definition: two or more threads read and write a shared variable, and final result depends on the order of the execution of those threads • Usually timing-dependent and intermittent – Hard to debug Technically not a race condition if all execution orderings 11/4/2011 CS 3214 Fall 2011 6 Technically not a race condition if all execution orderings lead to same result – Chances are high that you misjudge this • How to deal with race conditions: – Ignore (!?)
Background image of page 1

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

View Full DocumentRight Arrow Icon
2 Not Sharing: Duplication or Partitioning • Undisputedly best way to avoid race conditions – Always consider it first – Usually faster than alternative of sharing + protecting – But duplicating has space cost; partitioning can have management cost – Sometimes must share (B depends on A’s result) 11/4/2011 CS 3214 Fall 2011 7 •E x am p l e s : – Each thread has its own counter (then sum counters up after join()) – Every CPU has its own ready queue – Each thread has its own memory region from which to allocate objects • Truly ingenious solutions to concurrency involve a way to partition things people originally thought you couldn’t Thread-Local Storage • A concept that helps to avoid race conditions by giving each thread a copy of a certain piece of state • Recall: – All local variables are already thread-local • But their extent is only one function invocation! – All function arguments are also thread-local • But must pass them along call-chain
Background image of page 2
Image of page 3
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 / 6

Lecture19-multithreading-locking - Announcements Project 5...

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

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