Global interpreter lock

From Infogalactic: the planetary knowledge core
Jump to: navigation, search

Global interpreter lock (GIL) is a mechanism used in computer language interpreters to synchronize the execution of threads so that only one native thread can execute at a time.[1] An interpreter that uses GIL always allows exactly one thread to execute at a time, even if run on a multi-core processor. Some popular interpreters that have GIL are CPython and Ruby MRI.

Technical background concepts

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. Python and Ruby MRI use GILs.

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.

Benefits and 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 for long periods of time (which can release the lock on the GIL on that thread while it processes), 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]

Reasons for employing such a 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).

Examples

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]

See also

References

  1. Lua error in package.lua at line 80: module 'strict' not found.
  2. Lua error in package.lua at line 80: module 'strict' not found.
  3. Lua error in package.lua at line 80: module 'strict' not found.
  4. Python/C API Reference Manual: Thread State and the Global Interpreter Lock
  5. Lua error in package.lua at line 80: module 'strict' not found.