I am new to Python multithreading coding. I consult the manual for Lock Object usage and find the normal case is
g_mutex = Lock()
g_mutex.acquire()
#some code
g_mutex.release()
But the lock does not specify what variable or function it is going to lock? So does python automatically find all critical variables to be locked? What if I call some function to modify some variables?
The purpose of a
Lockis that at most one thread can hold it at any given time. If you acquire the lock, you can be sure no other process holds it. It’s up to you to define the semantics of the lock — if you want to use it to guard the access to some variable, just do so: acquire the lock before messing around with that variable, release it when you are done. There is no need for an explicit relation between the lock object and the variable it protects — this is defined by the way you use it. (Also note that there is nothing Python-specific to this concept.)