0
0
Rubyprogramming~20 mins

GIL (Global Interpreter Lock) impact in Ruby - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
GIL 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 threading code?

Consider the following Ruby code using threads. What will be printed?

Ruby
count = 0
threads = []
5.times do
  threads << Thread.new do
    1000.times { count += 1 }
  end
end
threads.each(&:join)
puts count
A1000
BA number less than 5000
C5000
DRaises a runtime error
Attempts:
2 left
💡 Hint

Think about how Ruby's GIL affects thread execution and shared variable updates.

🧠 Conceptual
intermediate
1:30remaining
Which statement best describes Ruby's GIL impact on CPU-bound threads?

Choose the statement that correctly describes how Ruby's GIL affects CPU-bound threads.

ARuby's GIL prevents true parallel execution of CPU-bound threads, limiting them to one core at a time.
BRuby's GIL allows multiple CPU-bound threads to run in parallel on multiple cores.
CRuby's GIL only affects IO-bound threads, not CPU-bound threads.
DRuby does not have a GIL, so threads run fully in parallel.
Attempts:
2 left
💡 Hint

Consider how the GIL serializes execution of Ruby code.

🔧 Debug
advanced
2:30remaining
Why does this Ruby code produce inconsistent results with threads?

Examine the code below. Why does the final printed value vary each run?

Ruby
total = 0
threads = []
10.times do
  threads << Thread.new do
    100.times { total += 1 }
  end
end
threads.each(&:join)
puts total
ABecause the variable 'total' is not thread-safe and increments cause race conditions.
BBecause the threads are not properly joined before printing.
CBecause the GIL locks the variable 'total' preventing any increments.
DBecause Ruby threads run in parallel on multiple cores causing synchronization issues.
Attempts:
2 left
💡 Hint

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

📝 Syntax
advanced
2:30remaining
Which Ruby code snippet correctly creates threads that safely increment a shared counter?

Choose the code that safely increments a shared counter variable using threads in Ruby.

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

Look for proper use of Mutex to avoid race conditions.

🚀 Application
expert
3:00remaining
How can Ruby programs achieve true parallelism despite the GIL?

Which approach allows Ruby programs to perform true parallel execution despite the GIL?

AUsing fibers to switch between tasks cooperatively.
BUsing multiple Ruby threads to run CPU-bound tasks concurrently.
CUsing multiple processes with inter-process communication instead of threads.
DDisabling the GIL in Ruby interpreter settings.
Attempts:
2 left
💡 Hint

Think about how to bypass the GIL's single-thread execution limitation.