Bird
Raised Fist0

In this code snippet, what is the main error?

medium๐Ÿ“ Analysis Q6 of Q15
LLD - Advanced LLD Concepts
In this code snippet, what is the main error?
lock.acquire()
if shared_var == 0:
    shared_var = 1
lock.release()
ALock is not released if an exception occurs
BLock is released before modifying shared_var
CLock is not acquired before checking shared_var
DLock is held during the entire check and update, which is correct
Step-by-Step Solution
Solution:
  1. Step 1: Review lock usage

    Lock is acquired and released properly in normal flow.
  2. Step 2: Consider exceptions

    If an exception occurs between acquire and release, lock may never release.
  3. Final Answer:

    Lock is not released if an exception occurs -> Option A
  4. Quick Check:

    Exception safety = release lock properly [OK]
Quick Trick: Use try-finally to always release locks [OK]
Common Mistakes:
MISTAKES
  • Ignoring exception safety
  • Assuming lock auto-releases on error

Want More Practice?

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

Free Signup - Practice All Questions
More LLD Quizzes