Bird
0
0

Identify the error in this Ruby code that tries to use a Mutex:

medium📝 Debug Q6 of 15
Ruby - Concurrent Programming
Identify the error in this Ruby code that tries to use a Mutex:
mutex = Mutex.new
mutex.lock
# critical section
mutex.lock
mutex.unlock
AMutex cannot be locked manually.
BMutex.new is incorrect syntax.
CMissing synchronize block.
DCalling lock twice without unlock causes deadlock.
Step-by-Step Solution
Solution:
  1. Step 1: Review mutex lock usage

    Locking a mutex twice without unlocking causes the thread to wait forever (deadlock).
  2. Step 2: Identify error in code

    Code locks mutex twice in a row without unlocking in between, causing deadlock.
  3. Final Answer:

    Calling lock twice without unlock causes deadlock. -> Option D
  4. Quick Check:

    Double lock without unlock = deadlock [OK]
Quick Trick: Lock once before unlock to avoid deadlock [OK]
Common Mistakes:
  • Thinking Mutex.new is wrong
  • Believing synchronize block is mandatory
  • Assuming mutex locks automatically

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes