GIL lock and user lock

GIL lock and user lock (Global Interpreter Lock Global Interpreter Lock)

1. Global Interpreter Lock: to ensure that only one thread has agreed time operating authority for resources

 

Role: in a process, only one thread at a time by GIL lock is called CPU, switching condition: I / O operations, fixed time * * (determined by the system) **.

Description: Python GIL lock in multithreading is the only CPU serial in operation, others are parallel, so much faster than serial.

1. In order to address different threads access the same resource at the same time, data protection issues arising from the GIL.

2.GIL in the interpreter's level limits the program has only one thread is actually executed by the CPU at the same time, regardless of your program actual number of open threads

3. To solve this problem, CPthon own definition of a global interpreter lock, at the same time only one thread can get this data.

4.python This happens because of a bad situation is to enable a python thread is to call the operating system native threads, is the C interface.

5. But this is only CPython this version of the problem, in PyPy, in that there is no kind of defects.

User lock: Lock thread:

The current thread has not been completed before other operations can not be more than a thread of its operations, and has released the GIL lock

1. When there GIL lock Why do I need a user locks ?

GIL lock can only guarantee the same time only one thread for each resource operation, but the completion of the current thread computing initiative to release the lock, other threads to its operation.

2. Thread the lock principle

When a thread operation of the CPU calculation of a resource plus a thread lock, completed only the current thread computing initiative to release the lock, other threads to its operation.

This prevents the calculation has not been completed, after the release of other threads GIL lock on this resource operations lead to confusion.

3. Use thread-locking principle to solve the above problems?

In the GIL lock together with a thread-locking thread lock is a user-level lock.

Thread is a thread lock on the data before the operation plus a lock, which prevents other threads law or this data,

Only the thread after the operation is completed the data will release the lock, other threads to manipulate the data.

Deadlock

1. The definition of deadlock

1. with a waiting another phenomenon caused due to competition for resources.

2. deadlock example


 1. Start 5 threads, run method, if the first grab thread1 A lock case thread1 A lock is not released, followed by executing code mutexB.acquire (), B grab lock, when the lock grab B, no other thread thread1 competition, because A lock is not released, the other threads can only wait.

   3. Using recursive lock to solve deadlock

1. The role of recursive lock is the same thread multiple requests for the same resource, but not parameters deadlock.

2. The internal RLock maintains a Lock and a counter variable. counter records the number of times acquire, so that resources can be many times require.

3. until all the threads are acquire a release, other threads to get resources.   

 

 

Guess you like

Origin www.cnblogs.com/junjun511/p/11298848.html