Download presentation
Presentation is loading. Please wait.
1
Thread Synchronization
Section 5 Thread Synchronization February 17th, 2016 Taught by Joshua Don
2
Project Tips Keep the spec and your design doc handy
Make a plan for breaking up the work Pair programming, group programming Work backwards from the due date to decide when you want to complete each task by You have the source code for all the tests Make a separate branch (don’t always push to master!) git checkout –b dev git checkout master git branch -v git merge dev
3
Project Tips Cont. Task difficulty
Really important files for proj1 that you should understand completely: timer.c synch.c/h thread.c/h Easy Hard Task difficulty Efficient alarm clock Priority scheduling + priority donation MLFQS Staff solution: 500 lines Personal: 363 lines
4
Atomic operations Atomic operation: An operation that either executes completely or not at all. Cannot be interrupted in the middle of performing its function. Is i++ atomic? load i increment value store i Race condition
5
Atomic operations i=0; i++; Thread 1 Thread 2 load i increment value
store i Race condition
6
Atomic operations i=0; i++; Thread 2 load i Thread 1 increment value
store i Thread 1 Race condition
7
Atomic operations i=0; i++; Thread 2 load i increment value store i
Race condition Thread 1
8
Atomic operations i=0; i++; load i Thread 2 increment value store i
Race condition Thread 1
9
Atomic operations i=0; i++; load i increment value store i Thread 1
Race condition Thread 1 Thread 2
10
Atomic operations i=0; i++; load i increment value store i Thread 2
Race condition Thread 2 Thread 1
11
Atomic operations i=0; i++; load i increment value store i Thread 1
Race condition Thread 1 Thread 2
12
Atomic operations i=1 i=0; i++; load i increment value store i
Race condition Thread 1 Thread 2 i=1
13
Test and Set Lock acquire Lock release
Test_and_set: An atomic operation that can be used to build a lock. int test_and_set(int *value) { int result = *value; *value = 1; return result; } Simple lock using test_and_set Busy wait int value = 0; ... while(test_and_set(&value)); // critical section value = 0; Lock acquire Lock release
14
Critical Sections Critical Section: A protected region of code. It requires mutual exclusion of access. i=0 lock_acquire(my_lock); i++; lock_release(my_lock);
15
Demo
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.