Synchronous lock lock lock recursion

Multithreading

Genlock (mutex)

Solve the problem?

Genlock solve the problem?

Multiple threads operate on the same data, data disorder problem may occur because one thread to get the data back, not enough time to operate on the data, cpu it is possible to execute another thread, another thread is to get data before the thread is not processed, as follows

import threading
import time
count=20

def zx():

    global count
    print(count)
    time.sleep(0.001)
    count-=1

t_list=[]
for t in range(20):
    t=threading.Thread(target=zx)
    t_list.append(t)
    t.start()

for i in t_list:
    i.join()

print(count)

20
20
20
20
20
20
18
18
18
15
15
15
13
11
11
8
8
8
6
6
0

Solution

You can force the cpu After you perform part of the code, in order to call another thread, so you can ensure that other threads get the correct data you want, but this part is serial.

Specific implementation process

A. 100 threads try to steal the GIL lock, that grab execute permissions

II. There must be a thread to grab GIL (for the time being called threads 1), and begins execution, once executed will get lock.acquire ()

III. Most likely has not finished running thread 1, 2 there is another thread to grab GIL, and then began to run, but found that a mutex lock Thread 2 Thread 1 has not yet been released, then blocked, forced to surrender execute permissions that release GIL

IV. Until the thread 1 again grabbed the GIL, execution continues from the last pause position until the normal release the mutex lock, then other threads and then repeat the process 234

import threading
import time
count=20

lock=threading.Lock()

def zx():

    global count
    lock.acquire()
    print(count)
    time.sleep(0.001)
    count-=1
    lock.release()

t_list=[]
for t in range(20):
    t=threading.Thread(target=zx)
    t_list.append(t)
    t.start()

for i in t_list:
    i.join()

print(count)

20
19
18
17
16
15
14
13
12
11
10
9
8
7
6
5
4
3
2
1
0

Deadlock

The so-called deadlock: refers to the phenomenon of two or more processes or threads in the implementation process, a result of competition for resources caused by waiting for each other, in the absence of external force, they will not be able to promote it. At this time, say the system is in deadlock state or system to produce a deadlock, which is always in the process of waiting for another process called the deadlock, deadlock is as follows

For example, there are two threads, thread 1 got A lock, but he only got the B key in order to release the lock A, lock B Thread 2 got, he got A lock to lock to release B, the results of these two threads died

import threading
import time

class MyThreading(threading.Thread):

    def run(self):
        self.run1()
        self.run2()

    def run1(self):
        A.acquire()
        print(self.name+"拿到了锁A")
        B.acquire()
        print(self.name+"拿到了锁B")
        B.release()
        print(self.name+"释放了锁B")
        A.release()
        print(self.name+"释放了锁A")

    def run2(self):
        B.acquire()
        print(self.name+"拿到了锁B")
        time.sleep(2)
        A.acquire()
        print(self.name+"拿到了锁A")
        A.release()
        print(self.name+"释放了锁A")
        B.release()
        print(self.name+"释放了锁B")

if __name__ == '__main__':
    A=threading.Lock()
    B=threading.Lock()
    for t in range(5):
        t=MyThreading()
        t.start()

Thread-1 to get a lock A
Thread-1 to get a lock B
Thread-1 release lock B
Thread-1 A lock release
Thread-1 to get a lock B
the Thread A-2 to get the lock

How to resolve a deadlock

Recursive lock

Recursive lock

Equivalent to a block of code with a lot of locks, seen as a whole, when this block all the locks are released, in order to be other ways to get the lock or thread

The principle is very simple recursive lock, when the lock, the lock recursive reference count of +1 or -1 unlocked when the reference count is 0, can be locked other threads or get methods.

import threading
import time

class MyThreading(threading.Thread):

    def run(self):
        self.run1()
        self.run2()

    def run1(self):
        D.acquire()
        print(self.name+"D==1")
        D.acquire()
        print(self.name+"D==2")
        D.release()
        print(self.name+"D==1")
        D.release()
        print(self.name+"D==0")

    def run2(self):
        D.acquire()
        print(self.name+"D==1")
        time.sleep(2)
        D.acquire()
        print(self.name+"D==2")
        D.release()
        print(self.name+"D==1")
        D.release()
        print(self.name+"D==0")

if __name__ == '__main__':
    D=threading.RLock()
    for t in range(5):
        t=MyThreading()
        t.start()

这里的==被转义的,将就着看
Thread-1D==1
Thread-1D==2
Thread-1D==1
Thread-1D==0
Thread-1D==1
Thread-1D==2
Thread-1D==1
Thread-1D==0
Thread-2D==1
Thread-2D==2
Thread-2D==1
Thread-2D==0
Thread-2D==1
Thread-2D==2
Thread-2D==1
Thread-2D==0
Thread-4D==1
Thread-4D==2
Thread-4D==1
Thread-4D==0
Thread-4D==1
Thread-4D==2
Thread-4D==1
Thread-4D==0
Thread-3D==1
Thread-3D==2
Thread-3D==1
Thread-3D==0
Thread-5D==1
Thread-5D==2
Thread-5D==1
Thread-5D==0
Thread-5D==1
Thread-5D==2
Thread-5D==1
Thread-5D==0
Thread-3D==1
Thread-3D==2
Thread-3D==1
Thread-3D==0

Guess you like

Origin www.cnblogs.com/zx125/p/11442945.html