First, before we compare, we need to know what the global interpreter lock is and what the mutex is, and what they are used to do to solve the problem.
First, the execution of the global interpreter lock Gil,python code is controlled by the Python virtual machine (also known as the interpreter main loop, CPython version), and Python is designed to take into account the main loop of the interpreter while only one thread is executing, that is, at any moment, Only one thread is running in the interpreter. Access to the Python virtual machine is controlled by the global Interpreter Lock (GIL), which ensures that only one thread is running at the same time. From the macroscopic microscopic, the Gil is the macroscopic operation. For example, in a 4-core environment, only one nucleus is running the thread, while the other three cores are empty. The Gil is a thread lock, for threads, not processes. However, these rules are given by CPython, and there is no such mechanism as Jpython.
Where, in the case of multithreading, the Python virtual machine executes as follows:
1. Set Gil
2. Switch to a thread to run
3. Run:
A. Specify the number of bytecode directives, or B. Thread active yield control (can call Time.sleep (0))
4. Set the thread to sleep
5. Unlock Gil
6. Repeat all of the above steps next is the mutex: When a thread wants to share data, it locks it, the state of the resource is locked, the other thread cannot change, until the thread frees the resource, the state of the resource becomes "non-locked", and the other thread can lock the resource again. The mutex ensures that only one thread enters the write operation at a time, thus guaranteeing the correctness of the data in multithreaded situations. A mutex is a synchronous mechanism whereby the thread frees the resource as a condition that the condition is completed before the next operation can be performed. is micro-operation. It seems that this will explain the difference between the two ... Follow-up supplement--
Is it necessary to have an inter-lock in Python?