1. The Critical Section
Definition: A segment of code where multiple threads access and modify the exact same shared variables or resources (like a shared database row).
- Threads have private local memory (their own registers), but they share global memory (the Heap).
- If two threads enter a Critical Section simultaneously, they will overwrite each other's work.
2. Race Conditions
Definition: A massive bug that occurs when the output of a program depends entirely on the unpredictable timing of the OS CPU Scheduler (Context Switches).
- If Thread 1 reads a balance of $100, and is instantly interrupted by the OS before it can add money, Thread 2 might also read $100. They both add $50, and both write back $150.
- $50 has magically vanished because they raced to overwrite the same memory address.
3. Mutexes (Mutual Exclusion)
Definition: A software "Key" or lock. Before entering a Critical Section, a thread must grab the Mutex. Only one thread can hold the key at a time.
- Spinlock: If the key is taken, the thread violently loops (`while(locked)`) burning CPU cycles until the key is dropped. Good for very short waits.
- Sleep Lock (Semaphore): If the key is taken, the OS intervenes, suspends the thread, moves it to a "Blocked Queue," and wakes it up later. Saves CPU, but requires a heavy context switch.