Python Threading thread / mutex / deadlocks / GIL lock

Import thread package

import threading

Ready thread function, pass parameters

t1 = threading.Thread(target=func,args=(args,))

Class inheritance thread, the thread object is created

class MyThread(threading.Thread)
    def run(self):
        pass

if __name__ == "__main__":
    t = MyThread()
    t.start()

Threads share fully variable, but in a shared global variable data errors occur problems
using the Lock class threading module, add mutex can solve the problem threads share global variables

# Create a lock 
mutex = threading.Lock () 

# locked 
mutex.acquire () 

# release lock 
mutex.release ()

Mutex could lead to deadlock
share resources among multiple threads of time, if two threads each occupy a portion of the resources, and at the same time waiting for each other's resources, will result in a deadlock.
Solution:
1. banker's algorithm : like a good time to lock and release the programming time and space relations
2. Add a timeout waiting

In multi-threaded Python Global Interpreter Lock GIL 
GIL is a legacy of the C language version of the python interpreter
GIL lock makes the python in the same time there is only one thread running
but closing fast multi-threaded or single-threaded, after all, this is because a thread IO blocking period of time, other threads can run
GIL lock and mutex is not the same, GIL lock is locked thread, the thread mutex lock within a transaction, a mutex is written by the developers themselves, GIL lock sources with the C version of the python interpreter

Means for Solving the GIL
1. python java interpreter version
2. Other language code, the completion of this portion

Guess you like

Origin www.cnblogs.com/xixu/p/11220724.html