Ruby - Concurrent ProgrammingWhich 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.unlockBmutex = Mutex.lock; mutex.synchronize { # critical code }Cmutex = Mutex.new; mutex.synchronize { # critical code }Dmutex = Mutex.lock; mutex.unlockCheck Answer
Step-by-Step SolutionSolution:Step 1: Recall Mutex usageMutex.new creates a mutex; synchronize method locks and unlocks automatically.Step 2: Check syntax correctnessmutex = Mutex.new; mutex.synchronize { # critical code } uses Mutex.new and synchronize block correctly. Others misuse methods or syntax.Final Answer:mutex = Mutex.new; mutex.synchronize { # critical code } -> Option CQuick 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.newForgetting to unlock after lockUsing lock without synchronize block
Master "Concurrent Programming" in Ruby9 interactive learning modes - each teaches the same concept differentlyLearnWhyDeepVisualTryChallengeProjectRecallTime
More Ruby Quizzes Concurrent Programming - Why concurrency matters in Ruby - Quiz 15hard Concurrent Programming - Ractor for true parallelism - Quiz 11easy Functional Patterns in Ruby - Lazy enumerators - Quiz 3easy Gems and Bundler - Bundler for dependency resolution - Quiz 8hard Gems and Bundler - Creating a gem basics - Quiz 2easy Regular Expressions - Match operator (=~) - Quiz 11easy Regular Expressions - Match method and MatchData - Quiz 14medium Regular Expressions - Regex literal syntax (/pattern/) - Quiz 2easy Testing with RSpec and Minitest - Minitest basics (assert style) - Quiz 1easy Testing with RSpec and Minitest - Minitest basics (assert style) - Quiz 11easy