ringa_lee2017-04-18 10:10:49
This is easier to understand. GIL ensures that python byte code only has one native thread running in the process at the same time. However, the logic of the byte code itself still requires locking/mutual exclusion, etc. to ensure that the thread can be reentrant, because the byte code may be interrupted at any point during the running process and switched to another thread.
A simple example:
// Step 1
counter = get_counter()
// Step 2
counter += 1
// Step 3
set_counter(counter)
Assume that the initial Counter is 0, and two threads execute this Python code at the same time without any synchronization.
But because there is no locking/mutual exclusion, the execution situation is as follows: