HOME

TheInfoList



OR:

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. 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 A multi-core processor (MCP) is a microprocessor on a single integrated circuit (IC) with two or more separate central processing units (CPUs), called ''cores'' to emphasize their multiplicity (for example, ''dual-core'' or ''quad-core''). Ea ...
, 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

A global interpreter lock (GIL) is a mutual-exclusion
lock Lock(s) or Locked may refer to: Common meanings *Lock and key, a mechanical device used to secure items of importance *Lock (water navigation), a device for boats to transit between different levels of water, as in a canal Arts and entertainme ...
held by a
programming language A programming language is a system of notation for writing computer programs. Programming languages are described in terms of their Syntax (programming languages), syntax (form) and semantics (computer science), semantics (meaning), usually def ...
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 A process is a series or set of activities that interact to produce a result; it may occur once-only or be recurrent or periodic. Things called a process include: Business and management * Business process, activities that produce a specific s ...
. 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

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.


Drawbacks

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. 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

Some language implementations that implement a global interpreter lock are CPython, the most widely-used implementation of Python, and Ruby MRI, the
reference implementation In the software development process, a reference implementation (or, less frequently, sample implementation or model implementation) is a program that implements all requirements from a corresponding specification. The reference implementation ...
of
Ruby Ruby is a pinkish-red-to-blood-red-colored gemstone, a variety of the mineral corundum ( aluminium oxide). Ruby is one of the most popular traditional jewelry gems and is very durable. Other varieties of gem-quality corundum are called sapph ...
(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 Microsoft Corporation is an American multinational corporation and technology company, technology conglomerate headquartered in Redmond, Washington. Founded in 1975, the company became influential in the History of personal computers#The ear ...
's Dynamic Language Runtime and also avoid using a GIL. An example of an interpreted language without a GIL is Tcl, which is used in the benchmarking tool
HammerDB HammerDB is an open source database benchmarking application developed by Steve Shaw. HammerDB supports databases such as Oracle, SQL Server, Db2, MySQL and MariaDB. HammerDB is written in Tcl, TCL and C (programming language), C, and is license ...
.


Example code

Example code in Python. Notice how a lock is acquired and released between each instruction call. It uses the object from the module. from threading import Lock INSTRUCTION_TABLE = def execute(bytecode: list) -> None: """Execute bytecode.""" lock = Lock() for (opcode, args) in bytecode: lock.acquire() INSTRUCTION_TABLE pcodeargs) lock.release()


See also

* Green threads * Giant lock


References

{{Reflist, 2 Articles with example Python (programming language) code Python (programming language) Concurrency control