0
0
Rubyprogramming~20 mins

Thread synchronization with Mutex in Ruby - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Mutex Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Ruby code using Mutex?

Consider the following Ruby code that uses a Mutex to synchronize threads incrementing a shared counter.

require 'thread'
counter = 0
mutex = Mutex.new
threads = []

10.times do
  threads << Thread.new do
    100.times do
      mutex.synchronize do
        counter += 1
      end
    end
  end
end

threads.each(&:join)
puts counter

What will be printed?

Ruby
require 'thread'
counter = 0
mutex = Mutex.new
threads = []

10.times do
  threads << Thread.new do
    100.times do
      mutex.synchronize do
        counter += 1
      end
    end
  end
end

threads.each(&:join)
puts counter
A0
BA number less than 1000 due to race conditions
CAn error because counter is not thread-safe
D1000
Attempts:
2 left
💡 Hint

Think about what the Mutex does to the shared variable.

Predict Output
intermediate
2:00remaining
What happens if Mutex is removed from this Ruby code?

Look at this Ruby code where multiple threads increment a shared variable without a Mutex:

counter = 0
threads = []

10.times do
  threads << Thread.new do
    100.times do
      counter += 1
    end
  end
end

threads.each(&:join)
puts counter

What is the most likely output?

Ruby
counter = 0
threads = []

10.times do
  threads << Thread.new do
    100.times do
      counter += 1
    end
  end
end

threads.each(&:join)
puts counter
AA number less than 1000 due to race conditions
B1000
CAn error because counter is not thread-safe
D0
Attempts:
2 left
💡 Hint

Think about what happens when multiple threads change the same variable without protection.

🔧 Debug
advanced
2:00remaining
Identify the error in this Ruby Mutex usage

Find the error in this Ruby code snippet that tries to use a Mutex to protect a shared resource:

mutex = Mutex.new
counter = 0

Thread.new do
  mutex.lock
  counter += 1
end

mutex.unlock
puts counter
Ruby
mutex = Mutex.new
counter = 0

Thread.new do
  mutex.lock
  counter += 1
end

mutex.unlock
puts counter
Acounter is not initialized before use
BMissing mutex.synchronize block causes race condition
CCalling mutex.unlock outside the thread causes a ThreadError
DThread never starts because of missing join
Attempts:
2 left
💡 Hint

Think about which thread owns the lock when unlocking.

📝 Syntax
advanced
2:00remaining
Which Ruby code correctly uses Mutex to protect a shared array?

Choose the Ruby code snippet that correctly uses a Mutex to safely append to a shared array from multiple threads.

A
mutex = Mutex.new
arr = []
threads = []
10.times do
  threads &lt;&lt; Thread.new do
    mutex.synchronize { arr &lt;&lt; 1 }
  end
end
threads.each(&amp;:join)
puts arr.size
B
mutex = Mutex.new
arr = []
threads = []
10.times do
  threads &lt;&lt; Thread.new do
    mutex.synchronize do
      arr.push(1)
    end
  end
end
threads.each(&amp;:join)
puts arr.size
C
mutex = Mutex.new
arr = []
threads = []
10.times do
  threads &lt;&lt; Thread.new do
    mutex.lock
    arr &lt;&lt; 1
  end
  mutex.unlock
end
threads.each(&amp;:join)
puts arr.size
D
mutex = Mutex.new
arr = []
threads = []
10.times do
  threads &lt;&lt; Thread.new do
    arr &lt;&lt; 1
  end
end
threads.each(&amp;:join)
puts arr.size
Attempts:
2 left
💡 Hint

Look for proper use of synchronize and thread joining.

🚀 Application
expert
3:00remaining
How many items will the shared hash contain after this Ruby threaded code?

Consider this Ruby code that uses a Mutex to safely add keys to a shared hash from multiple threads:

require 'thread'
shared_hash = {}
mutex = Mutex.new
threads = []

5.times do |i|
  threads << Thread.new do
    10.times do |j|
      mutex.synchronize do
        shared_hash["key_#{i}_#{j}"] = i * j
      end
    end
  end
end

threads.each(&:join)
puts shared_hash.size

What will be printed?

Ruby
require 'thread'
shared_hash = {}
mutex = Mutex.new
threads = []

5.times do |i|
  threads << Thread.new do
    10.times do |j|
      mutex.synchronize do
        shared_hash["key_#{i}_#{j}"] = i * j
      end
    end
  end
end

threads.each(&:join)
puts shared_hash.size
A5
B50
C10
D0
Attempts:
2 left
💡 Hint

Count how many unique keys are added by all threads combined.