Is it necessary to have an inter-lock in Python?

Source: Internet
Author: User
Tags mutex

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?

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.