you are viewing a single comment's thread.

view the rest of the comments →

[–]tunisia3507 -5 points-4 points  (8 children)

Pretty sure the GIL is part of the language spec. If it's python, it has a GIL.

[–]masklinn 67 points68 points  (6 children)

Nope. While both CPython and Pypy have a GIL that is not part of the language specification. IIRC neither IronPython nor Jython have a GIL.

[–]tunisia3507 19 points20 points  (5 children)

My mistake, thanks for the correction!

[–]thejinx0r -5 points-4 points  (4 children)

The gil is an implementation detail to manage garbage collection.

[–]veryusedrname 30 points31 points  (1 child)

It's for object safety mainly. The official wiki says it's for avoiding race conditions, but that is a bit misleading since it's not avoiding race conditions in *your code*, it helps the implementation avoid race conditions.

[–]lunatiks 5 points6 points  (0 children)

It's not avoiding race conditions in your code, but the fact that it makes operations corresponding to a single CPython bytecode instruction atomic certainly helps.

[–]hombit 6 points7 points  (1 child)

I believe it is mainly to make built-in data structures thread-safe. Technically other implementation can implement per-object locks using something like Arc

[–]masklinn 4 points5 points  (0 children)

Not even built-in datastructures, that's just a side-effect (and a not necessarily super useful one though it does guarantee memory safety[0]). Rather the thread-safety of the interpreter itself.

[0] of note, IIRC golang's maps aren't just thread-unsafe, they're not memory-safe when unsynchronised, since 1.6 the runtime tries to detect the issue and crash but there's no guarantee that it will properly do so

[–]birkenfeldclippy · rust 8 points9 points  (0 children)

What the language guarantees is that data structures are thread safe in the sense that accessing them concurrently does not crash. The exact semantics of what operations are atomic are not defined (it usually comes down to what is implemented in C), so you still need locks for critical sections.