The GIL locks cores so that threads cannot run in parallel. Why is this the case? There is little information about this online.

- 11,201
- 10
- 62
- 89

- 41
- 4
-
1https://wiki.python.org/moin/GlobalInterpreterLock – Alex Mar 03 '18 at 23:43
-
Thread safety is hard and they didn't design for it from the beginning. Making it thread safe now is even harder. – user2357112 Mar 03 '18 at 23:48
1 Answers
In very basic terms the GIL stops memory corruption, without GIL it is possible that multiple threads would execute at once causing unpredictable outputs.
In CPython, the global interpreter lock, or GIL, is a mutex that protects access to Python objects, preventing multiple threads from executing Python bytecodes at once. This lock is necessary mainly because CPython's memory management is not thread-safe. (However, since the GIL exists, other features have grown to depend on the guarantees that it enforces.)
See the documentation
As you will see there are implementations do not use GIL such as Jython and IronPython.
There is also a very helpful article on Wikipedia that deals with this topic.
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.

- 11,201
- 10
- 62
- 89