Global interpreter lock

Mechanism that ensures threads are not executed in parallel

Schematic representation of how threads work under GIL. Green - thread holding GIL, red - blocked threads

A global interpreter lock (GIL) is a mechanism used in computer-language interpreters to synchronize the execution of threads so that only one native thread (per process) can execute basic operations (such as memory allocation and reference counting) at a time.[1] As a general rule, an interpreter that uses GIL will see only one thread to execute at a time, even if it runs on a multi-core processor, although some implementations provide for CPU intensive code to release the GIL, allowing multiple threads to use multiple cores. Some popular interpreters that have a GIL are CPython and Ruby MRI.

## Technical background concepts

[edit]

A global interpreter lock (GIL) is a mutual-exclusion lock held by a programming language interpreter thread to avoid sharing code that is not thread-safe with other threads. In implementations with a GIL, there is always one GIL for each interpreter process.

Applications running on implementations with a GIL can be designed to use separate processes to achieve full parallelism, as each process has its own interpreter and in turn has its own GIL. Otherwise, the GIL can be a significant barrier to parallelism.

## Advantages

[edit]

Reasons for employing a global interpreter lock include:

* increased speed of single-threaded programs (no necessity to acquire or release locks on all data structures separately),
* easy integration of C libraries that usually are not thread-safe,
* ease of implementation (having a single GIL is much simpler to implement than a lock-free interpreter or one using fine-grained locks).

A way to get around a GIL is creating a separate interpreter per thread, which is too expensive with most languages.[citation needed]

## Drawbacks

[edit]

Use of a global interpreter lock in a language effectively limits the amount of parallelism reachable through concurrency of a single interpreter process with multiple threads. If the process is almost purely made up of interpreted code and does not make calls outside of the interpreter which block for long periods of time (allowing the GIL to be released by that thread while they process), there is likely to be very little increase in speed when running the process on a multiprocessor machine. Due to signaling with a CPU-bound thread, it can cause a significant slowdown, even on single processors.[2] More seriously, when the single native thread calls a blocking OS process (such as disk access), the entire process is blocked, even though other application threads may be waiting.

## Examples

[edit]

Some language implementations that implement a global interpreter lock are CPython, the most widely-used implementation of Python,[3][4] and Ruby MRI, the reference implementation of Ruby (where it is called Global VM Lock).

JVM-based equivalents of these languages (Jython and JRuby) do not use global interpreter locks. IronPython and IronRuby are implemented on top of Microsoft's Dynamic Language Runtime and also avoid using a GIL.[5]

An example of an interpreted language without a GIL is Tcl, which is used in the benchmarking tool HammerDB.[6]

## Example code

[edit]

Example code in Python. Notice how a lock is acquired and released between each instruction call. It uses the Lock object from the threading module.[7]

from threading import Lock

INSTRUCTION_TABLE = { ... }

def execute(bytecode: list) -> None:
"""Execute bytecode."""
lock = Lock()
for (opcode, args) in bytecode:
lock.acquire()
INSTRUCTION_TABLE[opcode](args)
lock.release()

## See also

[edit]

* Green threads
* Giant lock

## References

[edit]

1. ^ "GlobalInterpreterLock". Retrieved 30 November 2015.
2. ^ David Beazley (2009-06-11). "Inside the Python GIL" (PDF). Chicago: Chicago Python User Group. Retrieved 2009-10-07.
3. ^ Shannon -jj Behrens (2008-02-03). "Concurrency and Python". Dr. Dobb's Journal. p. 2. Retrieved 2008-07-12. "The GIL is a lock that is used to protect all the critical sections in Python. Hence, even if you have multiple CPUs, only one thread may be doing "pythony" things at a time."
4. ^ "Python/C API Reference Manual: Thread State and the Global Interpreter Lock". Archived from the original on 2008-09-14. Retrieved 2014-08-15.
5. ^ "IronPython at python.org". python.org. Retrieved 2011-04-04. "IronPython has no GIL and multi-threaded code can use multi core processors."
6. ^ "HammerDB Concepts and Architecture". HammerDB. 2018-11-30. Retrieved 2020-05-10. "It is important to understand at the outset that HammerDB is written in TCL because of the unique threading capabilities that TCL brings."
7. ^ "threading — Thread-based parallelism". Python documentation. Retrieved 16 April 2025.