0
0
Rubyprogramming~20 mins

Why concurrency matters in Ruby - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Ruby Concurrency Master
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 threads?

Consider this Ruby code that creates two threads incrementing a shared counter without synchronization. What will be the output?

Ruby
counter = 0
threads = []
2.times do
  threads << Thread.new do
    1000.times { counter += 1 }
  end
end
threads.each(&:join)
puts counter
A2000
B1000
CA number less than 2000
DRaises a ThreadError
Attempts:
2 left
💡 Hint

Think about what happens when two threads update the same variable without locks.

🧠 Conceptual
intermediate
1:30remaining
Why does Ruby MRI have a Global Interpreter Lock (GIL)?

Why does the standard Ruby interpreter (MRI) use a Global Interpreter Lock (GIL)?

ATo prevent race conditions by allowing only one thread to execute Ruby code at a time
BTo allow multiple threads to run Ruby code truly in parallel
CTo speed up IO operations by running threads concurrently
DTo enable garbage collection to run in parallel with Ruby code
Attempts:
2 left
💡 Hint

Think about how MRI manages thread safety internally.

Predict Output
advanced
2:00remaining
What does this Ruby code output with concurrent fibers?

Given this Ruby code using fibers, what will be printed?

Ruby
fiber1 = Fiber.new do
  puts 'Fiber 1 start'
  Fiber.yield
  puts 'Fiber 1 resume'
end
fiber2 = Fiber.new do
  puts 'Fiber 2 start'
  Fiber.yield
  puts 'Fiber 2 resume'
end
fiber1.resume
fiber2.resume
fiber1.resume
fiber2.resume
AFiber 1 start\nFiber 2 start\nFiber 1 resume
BFiber 1 start\nFiber 1 resume\nFiber 2 start\nFiber 2 resume
CFiber 1 start\nFiber 2 start\nFiber 2 resume\nFiber 1 resume
DFiber 1 start\nFiber 2 start\nFiber 1 resume\nFiber 2 resume
Attempts:
2 left
💡 Hint

Remember that Fiber.yield pauses the fiber and resume continues it.

🔧 Debug
advanced
1:30remaining
Why does this Ruby thread code raise an error?

Look at this Ruby code snippet. Why does it raise a ThreadError?

Ruby
thread = Thread.new do
  sleep 1
end
thread.run
thread.run
ABecause the thread is already running and <code>run</code> is called again causing a deadlock
BBecause <code>Thread#run</code> cannot be called twice on the same thread
CBecause <code>Thread#run</code> is deprecated and raises an error
DBecause <code>Thread#run</code> can only be called from the main thread
Attempts:
2 left
💡 Hint

Check Ruby docs for Thread#run behavior.

🚀 Application
expert
3:00remaining
How to safely increment a shared counter in Ruby threads?

You want to increment a shared counter from multiple Ruby threads safely. Which code snippet correctly uses synchronization to avoid race conditions?

A
counter = 0
mutex = Mutex.new
threads = 2.times.map do
  Thread.new do
    1000.times do
      mutex.synchronize { counter += 1 }
    end
  end
end
threads.each(&amp;:join)
puts counter
B
counter = 0
threads = 2.times.map do
  Thread.new do
    1000.times do
      counter += 1
    end
  end
end
threads.each(&amp;:join)
puts counter
C
counter = 0
mutex = Mutex.new
threads = 2.times.map do
  Thread.new do
    mutex.synchronize do
      1000.times { counter += 1 }
    end
  end
end
threads.each(&amp;:join)
puts counter
D
counter = 0
threads = 2.times.map do
  Thread.new do
    1000.times do
      Thread.pass
      counter += 1
    end
  end
end
threads.each(&amp;:join)
puts counter
Attempts:
2 left
💡 Hint

Think about locking only the increment operation, not the whole loop.