Jump to content

Global interpreter lock

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Wfunction (talk | contribs) at 14:38, 28 June 2012 (Concurrency is not the same as parallelism. Concurrency means overlapping operations; parallelism means operations happening simultaneously.). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

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 languages with a GIL, there is always one GIL for each interpreter process. CPython and CRuby use GILs.

Applications written in programming languages 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—a price paid for having the dynamism of the language.

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.[1]

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.

Examples

Some language implementations that implement a Global Interpreter Lock are CPython, the most widely used implementation of Python[2][3], 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.[4][citation needed]

References

  1. ^ David Beazley (2009-06-11). "Inside the Python GIL" (PDF). Chicago: Chicago Python User Group. Retrieved 2009-10-07. {{cite web}}: External link in |publisher= (help)
  2. ^ 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.
  3. ^ Python/C API Reference Manual: Thread State and the Global Interpreter Lock
  4. ^ "IronPython at python.org". python.org. Retrieved 2011-04-04. IronPython has no GIL and multi-threaded code can use multi core processors.

See also