Home > other >  Python multithreaded extremely novice locking problems
Python multithreaded extremely novice locking problems

Time:09-22

The from threading import Thread, Lock

Dicts={30} "a" :

Def gai () :
Lock the lock ()=
Lock. Acquire ()
Dicts [" a "]=(dicts [" a "] 1)
Print (dicts [" a "])
The lock release ()

Def gai1 () :
For I in range (10) :
Gai Gai=()

Def gai2 () :
For I in range (10) :
Gai Gai=()

Def gai3 () :
For I in range (10) :
Gai Gai=()

If __name__=="__main__" :
P1=Thread (target=gai1, args=())
The p2=Thread (target=gai2, args=())
P3=Thread (target=gai3, args=())
P1. The start ()
P2. The start ()
P3. Start ()
P1. The join ()
P2. The join ()
P3. The join ()


This code is the final output or data corruption, is my locked position is wrong

CodePudding user response:

Multi-threaded display confusion is normal, because it is a different thread to rob resources, who first to who first used, so there is no order, I don't know what you said is that kind of confusion, but there are two places I suggest you change the lock objects based on the global, additional dicts best statement is global variables
  • Related