En bref. Les threads Ruby chevauchent l’attente, pas le calcul.
Le modèle en une phrase
Le GVL n’autorise qu’un thread Ruby à la fois : il se libère pendant l’attente (I/O), jamais pendant le calcul.
In short. Ruby threads overlap waiting, not computing.
The model in one sentence
The GVL lets only one Ruby thread run at a time: it is released while waiting (I/O), never while computing.
4.times { sleep 0.5 }4 threads { sleep 0.5 }
4.times { sleep 0.5 }— Quatre attentes à la suite : environ 2 secondes.Four waits in a row: about 2 seconds.4 threads { sleep 0.5 }— Le GVL se libère pendant l'attente : les quatre se chevauchent, environ 0,5 s.The GVL is released while waiting: the four overlap, about 0.5 s.
La preuve, avec Ruby 3.4
Quatre tâches, mesurées avec Benchmark.realtime :
io = -> { sleep 0.5 }
cpu = -> { x = 0; 3_000_000.times { x += 1 }; x }
4.times { io.() } # ≈ 2,02 s
4.times.map { Thread.new { io.() } }.each(&:join) # ≈ 0,51 s
4.times { cpu.() } # ≈ 0,53 s
4.times.map { Thread.new { cpu.() } }.each(&:join) # ≈ 0,49 s
4.times.map { Ractor.new { x = 0; 3_000_000.times { x += 1 } } }.each(&:take) # ≈ 0,17 sL’attente se chevauche (2,02 s → 0,51 s), le calcul non (0,53 s → 0,49 s). Les Ractors, eux, parallélisent le calcul.
Ce que l’image cache
Ces durées sont celles d’une machine : l’ordre de grandeur compte, pas la décimale. Les Ractors sont expérimentaux en 3.4 (ils le signalent à l’exécution) et n’acceptent pas n’importe quel objet partagé. Une application réelle mélange attente et calcul : mesure ton goulot avant de choisir.
Pour aller plus loin
Le numéro ci-dessous détaille le GVL, les threads, les Ractors et les processus, avec des exercices.
The proof, with Ruby 3.4
Four tasks, measured with Benchmark.realtime:
io = -> { sleep 0.5 }
cpu = -> { x = 0; 3_000_000.times { x += 1 }; x }
4.times { io.() } # ≈ 2.02 s
4.times.map { Thread.new { io.() } }.each(&:join) # ≈ 0.51 s
4.times { cpu.() } # ≈ 0.53 s
4.times.map { Thread.new { cpu.() } }.each(&:join) # ≈ 0.49 s
4.times.map { Ractor.new { x = 0; 3_000_000.times { x += 1 } } }.each(&:take) # ≈ 0.17 sWaiting overlaps (2.02 s → 0.51 s), computing doesn’t (0.53 s → 0.49 s). Ractors do parallelize computation.
What the picture hides
These durations are from one machine: the order of magnitude matters, not the decimal. Ractors are experimental in 3.4 (they warn at runtime) and don’t accept just any shared object. A real application mixes waiting and computing: measure your bottleneck before choosing.
Go deeper
The issue below details the GVL, threads, Ractors and processes, with exercises.