InterviewBiz LogoInterviewBiz
← Back
Explain How Thread Synchronization Works and Why It Is Necessary
software-engineeringmedium

Explain How Thread Synchronization Works and Why It Is Necessary

MediumHotMajor: software engineeringmicrosoft, amazon, intel

Concept

Thread synchronization is the coordination mechanism that ensures multiple threads can safely access shared resources (such as variables, files, or databases) without corrupting data or causing race conditions.
Without synchronization, concurrent threads might interleave operations unpredictably, leading to inconsistent or incorrect program behavior.


1. Why Synchronization Is Needed

Threads often share data structures or memory to perform related tasks.
If two threads modify the same data simultaneously, results may become nondeterministic or incorrect.

Example (safe for MDX):

counter = 0
Thread A → counter += 1
Thread B → counter += 1
# Expected: 2, but actual result may be 1 due to overlapping updates.

This issue — called a race condition — happens when operations on shared data are not atomic.


2. Synchronization Techniques

MechanismDescriptionExample Languages
Mutex (Mutual Exclusion Lock)Allows only one thread to access a resource at a time.C, C++, Java, Go
SemaphoreControls access for a fixed number of threads (e.g., 3 threads at once).C, Python, Java
MonitorCombines lock + condition variables for structured synchronization.Java synchronized, C# lock
Condition VariableUsed for signaling between threads (wait/notify).C++, Java
Read-Write LocksAllow multiple readers but only one writer.Java ReentrantReadWriteLock

3. Mutex Example (safe for MDX)

mutex.lock()
try:
    shared_resource += 1
finally:
    mutex.unlock()

Only one thread holds the lock at a time, ensuring exclusive access. If another thread tries to acquire the same lock, it waits (blocks) until released.


4. Common Synchronization Problems

ProblemDescriptionMitigation
DeadlockTwo or more threads waiting indefinitely for each other’s locks.Consistent lock order, timeout locks
LivelockThreads keep responding to each other but make no progress.Backoff or randomized delay
StarvationOne thread never gets CPU or lock access.Fair locks or priority scheduling

5. Real-World Example

Scenario: Bank Transaction System

  • Thread A transfers funds from Account X to Y.
  • Thread B reads balance of Account X. If both run simultaneously without locks, Thread B might read an intermediate state (before the debit completes). Synchronization ensures atomicity and consistency of each operation.

6. Synchronization vs Parallelism

AspectSynchronizationParallelism
PurposeEnsure correctnessImprove performance
BehaviorRestrictive (prevents overlap)Expansive (enables overlap)
RiskDeadlock, contentionWork imbalance

Good system design balances both — synchronization for correctness, parallelism for speed.


7. Best Practices

  • Keep critical sections short to reduce contention.
  • Prefer immutable data structures where possible.
  • Use thread-safe collections instead of manual locking.
  • Avoid nested locks unless necessary.
  • Leverage atomic operations (AtomicInteger, compareAndSwap) for small updates.

8. Interview Tip

  • Clarify difference between data-level and task-level synchronization.
  • Be ready to describe mutex, semaphore, and monitor with examples.
  • Mention common pitfalls like deadlocks and how to avoid them.
  • If possible, discuss real implementations (e.g., POSIX threads, Java concurrency utilities).

Summary Insight

Synchronization is the guardrail of multithreading — it prevents chaos when threads share data, ensuring consistency, correctness, and sanity in concurrent systems.