Heap and stack memories
How do threads communicate?
Shared resources
Local variables as a shared resource?
Local object reference as a shared resource?
Context switch
Interleave
Atomic operation
Thread Interference
Thread safe
Avoiding thread interference
Race condition
Synchronization
Sync Patterns
Mutual Exclusion
Condition sync
Critical Section
Four Requirements for a solution
Requirement for solution: Mutual Exclusion
Only one thread in the critical section at a time.
Requirement for solution: Progress
No dead/livelock. When no thread is in the CS, any thread that wants to enter should do so without delay.
Requirement for solution: Fairness
No starvation. A thread shouldn’t wait indefinitely to enter the CS. => Bounded Wait (upper bound on nr of times a thread is allowed to enter its CS while another thread is waiting).
Requirement for solution: No assumption on performance
Requirements must be met with any nr of CPUs with any relative speed. Overhead of entering & exiting should be small.
What is a “lock”?
object lockObj = new object();
lock (lockObj)
Lock states