The Global Interpreter Lock (GIL) is a fundamental constraint in Ruby’s threading model that ensures only a single thread executes Ruby code at any given moment.
The GIL exists primarily because many Ruby extension libraries are not thread-safe and rely on the GIL for protection. Without it, these extensions could corrupt memory or produce undefined behavior when accessed concurrently.
Impact on Concurrency
In Ruby Concurrency Mechanisms, standard threads operate under the GIL’s constraint. While threads use native operating system threads, the GIL prevents true Parallelism—only one thread runs Ruby code at a time. However, threads remain useful for O-bound work because threads waiting on I/O are considered idle, allowing other threads to execute.
Ractors: Per-Actor GILs
Ractors (introduced in Ruby 3.0) achieve true parallelism by giving each Ractor its own GIL. This allows multiple Ractors to execute Ruby code simultaneously across CPU cores. However, threads within a single Ractor still share that Ractor’s GIL, reintroducing the bottleneck at the actor level.
See Thread Contention is GVL Queuing in Ruby for details on how thread scheduling works under the GVL.