title: python threads - how do "condition.wait" and "condition.notifyAll" work
body: <p>I have the following "consumer" code:</p>
  
  <pre><code>    ....
  
      while 1:
  
          time.sleep(self.sleeptime)
  
          cond.acquire() #acquire the lock
          print currentThread(), "lock acquired"
  
          while itemq.isEmpty():
              cond.wait()
  
          itemq.consume()
          print currentThread(),"Consumed One Item"
          cond.release()
  </code></pre>
  
  <p>And the following producer's code:</p>
  
  <pre><code>....     
  while 1 :
  
  
             cond.acquire() #acquire the lock
             print currentThread(), "lock acquired"
             print currentThread(),"Produced One Item"
             itemq.produce()
             cond.notifyAll()
             cond.release()
  
             time.sleep(self.sleeptime)
  </code></pre>
  
  <p>I'm running the program with 1 producer and 2 consumers.
  I don't know what result to expect. The producer calls "notifyAll()", so I expect both consumers to wake up from their "wait". I see that indeed both consumer acquire the lock, but only the first one who acquired the lock actually get to consume the item. Could somebody please tell me how the "wait" command works? If both threads get the "notifyAll", how is it that only one gets to consume?</p>
  
  <p>Thanks,
  Li</p>