Use of threading.Condition class

Richard Brodie R.Brodie at rl.ac.uk
Thu Jan 4 09:24:41 EST 2001


<vonWedel at lfpt.rwth-aachen.de> wrote in message
news:mailman.978611884.20920.python-list at python.org...

>I'm trying to pass data between threads A and B. A notifies B to produce something
>and now I want the reference to the produced instance back in thread A.
>The documentation for threading.Condition proposes something like
>
>    while not an_item_is_available():
>         cv.wait()
>
>I don't understand the while loop around  the cv.wait() statement -- wouldn't
>cv.wait() block anyway until cv.notify() has been called in the other thread?

It's traditional defensive programming, I think. The pthread specification
permits threads blocked on condition variables to wake up, in the equivalent
construct. There is some deep optimization reason why it's like that apparently.

I suspect that it isn't strictly required in Python: it's certainly not clear from
the documentation that spurious wakeups can happen, and a quick review
of the code seems to support this.





More information about the Python-list mailing list