ics 332 (week 7) - synchronization: race conditions

0.0(0)
studied byStudied by 0 people
0.0(0)
full-widthCall with Kai
GameKnowt Play
New
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/23

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

24 Terms

1
New cards

what is the goal of thread synchronization?

- to make sure threads don't step on each other's toes in RAM to ensure program correctness

2
New cards

what is false concurrency?

- illusion of concurrency (because time quanta short)

- created by an OS using context-switching to alternate between processes/threads on a single core

3
New cards

what is true concurrency?

- when multiple threads actually run simultaneously on multiple processor cores

4
New cards

why shouldn't a programmer need to care if their program will use true or false concurrency?

- concurrent program should work correctly on any system & OS handles the transparency through CPU virtualization

5
New cards

how does the type of concurrency affect a multi-threaded program's interactivity?

- multi-threaded program will reach higher interactivity with both true AND false concurrency

6
New cards

how does the type of concurrency affect a multi-threaded program's performance?

- multi-threaded program will reach higher performance only with true concurrency (on multiple cores)

7
New cards

besides multiple CPU cores, what is another example of concurrency between hardware resources?

- concurrency between CPU and disk

8
New cards

what is the classic "pitfall" when programmers first try to use multiple threads?

- thinking that simply adding more threads to a task (like incrementing a counter) will automatically make it faster and work correctly

9
New cards

what is a race condition?

- concurrency bug

- when program's outcome depends on the unpredictable timing or order of execution of multiple threads/processes

10
New cards

what is a lost update?

- type of race condition concurrency bug

- when multiple threads/processes concurrently access & modify the same data --> overwrites which causes lost data

- typically happens in a shared system

11
New cards

what happens when multiple threads increment a shared counter without synchronization?

- race condition occurs

- final count will be incorrect & unpredictable because increments can be lost

12
New cards

why are race conditions particularly difficult to debug?

- non-deterministic (bug might not appear during testing but can suddenly occur for users, making it hard to reproduce)

13
New cards

what is a critical section?

- section(s) of code where only one thread can execute at a time (preventing threads from "stepping on each's toes")

14
New cards

what is the common misconception about what a critical section protects?

- it protects a variable (incorrect!)

- BUT it protects sections of code that access shared variables

15
New cards

what is the key principle for designing good critical sections?

- make short as possible (in execution time)

- use different locks for different variables to maximize concurrency

16
New cards

what are the three requirements for a critical section?

1. mutual exclusion: only one thread in the section at a time

2. progress: thread wanting to enter will eventually get in

3. bounded waiting: a waiting thread won't be starved indefinitely

17
New cards

why must preemptive kernels also worry about race conditions?

- context switch can happen during a system call while the kernel is updating shared data structures --> leading to kernel-level lost updates

18
New cards

what is the modern hardware solution that enables the implementation of locks?

- atomic instructions (CPU instructions that execute completely without being interrupted)

19
New cards

what is a lock and its two fundamental operations?

- synchronization data types that ensures only one thread can access a shared resource at a time, preventing race conditions

- has two states (taken/not taken)

1. acquire() or lock()

2. release() or unlock()

20
New cards

what is spinlock? advantages? disadvantages?

- thread actively waits in a loop ("spins") for a lock, consuming CPU

- advantage: very low overhead --> thread enters the critical section immediately

- disadvantages: won't know how long we spin (when is that lock coming?) —> wastes CPU cycles, power, and generates heat if the critical section is long, as the waiting thread loops continuously

21
New cards

what is blocking lock? advantages? disadvantages?

- thread is put to sleep by the OS and wakes up when key is available, freeing the CPU (wakes CPU up later)

- advantage: wastes zero CPU cycles --> waiting thread is put to sleep (ideal for long waits)

- disadvantages: high overhead --> cost of OS putting the thread to sleep and then waking it up again

22
New cards

when should you use a spinlock vs blocking lock?

- spinlock: very short critical sections

- blocking lock: long critical sections

23
New cards

what problem does Java's synchronized keyword solve?

- prevents the common bug of forgetting to call unlock(), as it automatically handles locking and unlocking

24
New cards

what is an adaptive lock?

- smart lock that tries to be a spinlock first (spins briefly) & if it doesn't get the lock quickly --> becomes a blocking lock