Bird
0
0

Which of the following is the correct syntax to create a Mutex and lock a critical section in Ruby?

easy📝 Syntax Q3 of 15
Ruby - Concurrent Programming
Which of the following is the correct syntax to create a Mutex and lock a critical section in Ruby?
Amutex = Mutex.new; mutex.lock; # critical code; mutex.unlock
Bmutex = Mutex.lock; mutex.synchronize { # critical code }
Cmutex = Mutex.new; mutex.synchronize { # critical code }
Dmutex = Mutex.lock; mutex.unlock
Step-by-Step Solution
Solution:
  1. Step 1: Recall Mutex usage

    Mutex.new creates a mutex; synchronize method locks and unlocks automatically.
  2. Step 2: Check syntax correctness

    mutex = Mutex.new; mutex.synchronize { # critical code } uses Mutex.new and synchronize block correctly. Others misuse methods or syntax.
  3. Final Answer:

    mutex = Mutex.new; mutex.synchronize { # critical code } -> Option C
  4. Quick Check:

    Mutex synchronize syntax = correct [OK]
Quick Trick: Use Mutex.new and synchronize block for safe locking [OK]
Common Mistakes:
  • Calling Mutex.lock instead of Mutex.new
  • Forgetting to unlock after lock
  • Using lock without synchronize block

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes