Mutexes are the simplest thread-synchronization mechanism, and Python-provided condition objects provide support for complex thread synchronization issues. condition is called a conditional variable and provides the wait and notify methods in addition to the acquire and release methods similar to lock. The thread first acquire a condition variable and then determines some conditions. Wait if the condition is not met, and if the condition is met, after some processing changes the condition, the other thread is notified by the Notify method, and the other thread in the wait state will be re-judged after receiving the notification. Constantly repeating this process to solve complex synchronization problems.
You can assume that the condition object maintains a lock (Lock/rlock) and a waiting pool. The thread obtains the condition object through acquire, and when the wait method is called, the thread releases the lock inside the condition and enters the blocked state while recording the thread in the waiting pool. When the Notify method is called, the condition object picks a thread from the waiting pool and notifies it to invoke the Acquire method to fetch the lock.
The constructor of the condition object can accept a Lock/rlock object as a parameter, and if not specified, the condition object creates a rlock on its own internally.
In addition to the Notify method, the Condition object provides a Notifyall method that notifies all threads in the waiting pool to attempt to acquire an internal lock. As a result of the above mechanism, threads in the waiting state can only be awakened by the Notify method, so the role of notifyall is to prevent the thread from ever being in a state of silence.
The classic problem of demonstrating conditional variable synchronization is producer and consumer issues: Suppose a group of producers (Producer) and a group of consumers (Consumer) interact with the product through a single market. The "strategy" of a producer is to produce 100 products on the market if there are less than 1000 remaining products in the market, while the consumer's "strategy" is to consume 3 products if the surplus in the market is more than 100. The code for solving producer and consumer problems with condition is as follows
Import threading
Import time
Class Producer (threading. Thread):
def run (self):
Global Count
While True:
If Con.acquire ():
If Count > 1000:
Con.wait ()
Else
Count = count+100
msg = self.name+ ' Produce, count= ' + str (count)
Print msg
Con.notify ()
Con.release ()
Time.sleep (1)
Class Consumer (threading. Thread):
def run (self):
Global Count
While True:
If Con.acquire ():
If Count < 100:
Con.wait ()
Else
Count = count-3
msg = self.name+ ' Consume 3, count= ' +str (count)
Print msg
Con.notify ()
Con.release ()
Time.sleep (1)
Count = 500
Con = threading. Condition ()
def test ():
For I in range (2):
p = Producer ()
P.start ()
For I in range (5):
c = Consumer ()
C.start ()
if __name__ = = ' __main__ ':
Test ()
Python multithreaded Programming (4): Conditional variable Synchronization