Module 2: Synchronization Flashcards
1
Q
Heap and stack memories
A
- Reference to an object + value type data is saved in the Stack. EX: the object ‘account’
- Object’s data is saved in the Heap. EX: ‘5000, 0.5, 12’ is saved in ‘account’.
- Every thread has its own program counter & needs its own calls stack.
2
Q
How do threads communicate?
A
- Through shared variables
- Through message passing / method calls
3
Q
Shared resources
A
- EX: Instance variables
- Multiple threads run the same method, work w/ diff values saved in the same variable & produce wrong results.
4
Q
Local variables as a shared resource?
A
- Every thread executing a method remembers the values stored in the local variables.
- When running the method at the same time = output correct.
5
Q
Local object reference as a shared resource?
A
- The reference itself is placed on the stack but object data is on the heap.
- Method is thread safe; every thread creates own instance of the object, the reference to the local object will be placed on the thread’s local stack.
- Just don’t make the local object available to another thread.
6
Q
Context switch
A
- Process of storing the state of a process or thread, so that it can be restored and resume execution at a later point.
- Threads may race to update & save the variable.
7
Q
Interleave
A
- Operation can consist of multiple steps.
- EX: k++;
1. Retrieve current value of k.
2. Increment value by 1, k = k+1
3. Store the incremented value back in k - Threads can overwrite each other by reading the initial value of k instead of the updated version.
8
Q
Atomic operation
A
- Performs completely w/o interleaving (i.e interference)
- Can’t stop in the middle. Either happens fully or doesn’t at all.
- k++; done completely by one thread before another accesses k.
9
Q
Thread Interference
A
- Condition when more than one thread, executing simultaneously, accesses the same piece of data.
- Caused by interleaving
- Data may get corrupted
- Occurs when the code is not THREAD SAFE.
10
Q
Thread safe
A
- Code is safe to call by multiple threads simultaneously.
- Non-interference: safety property. Guarantees that atomic action in a thread does not alter a shared resource.
11
Q
Avoiding thread interference
A
- Locks
- Const, readonly
- Thread-local variables
- Atomic operations (Mutual Exclusion)
12
Q
Race condition
A
- Threads race through the critical secion
- Sharing data & resources => Reading/Writing can occur in the wrong order. Erroneous output & corrupt data.
13
Q
Synchronization
A
- Process of making threads work in a way to ensure that shared data and resources are used safely.
- Possible interleaving is restricted.
- EX: locks, mutex, semaphores, monitors, condition variables.
14
Q
Sync Patterns
A
- Writer-Reader
- Producer-Consumer (bounded buffer)
15
Q
Mutual Exclusion
A
- Only one thread is allowed to be in its critical region at a certain time.
- ME is a mechanism that ensures this.
16
Q
Condition sync
A
- Wait, Pulse, PulseAll
- Signal a condition, process sets a shared variable
- One thread waits for an event signaled from another thread
- Wait for a condition to become true (called ‘busy waiting’)
16
Q
Critical Section
A
- Sequences of instructions that may get incorrect results if executed simultaneously
- Usually accesses shared variables
- Must be protected!!!
17
Q
Four Requirements for a solution
A
- Mutual Exclusion
- Progress
- Fairness
- No assumption on performance