Bird
Raised Fist0

Given this pseudocode for a shared counter increment:

medium๐Ÿ“ Analysis Q4 of Q15
LLD - Advanced LLD Concepts
Given this pseudocode for a shared counter increment:
lock.acquire()
counter = counter + 1
lock.release()

What happens if lock.release() is missing?
AOther threads may never access the counter again
BThe counter increments twice
CThe program crashes immediately
DThe lock automatically releases after increment
Step-by-Step Solution
Solution:
  1. Step 1: Understand lock usage

    lock.acquire() locks the resource; lock.release() frees it for others.
  2. Step 2: Analyze missing release effect

    If release is missing, the lock stays held, blocking other threads indefinitely.
  3. Final Answer:

    Other threads may never access the counter again -> Option A
  4. Quick Check:

    Missing release = blocked threads [OK]
Quick Trick: Always release locks to avoid thread blocking [OK]
Common Mistakes:
MISTAKES
  • Assuming lock auto-releases
  • Thinking missing release causes crash

Want More Practice?

15+ quiz questions ยท All difficulty levels ยท Free

Free Signup - Practice All Questions
More LLD Quizzes