Threading and Conditions

Konrad Hinsen hinsen at
Wed Mar 22 14:38:58 CET 2000

Ken Seehof <kens at> writes:

> Both notify() and wait() require a thread lock. By my understanding,
> this means that a notify cannot occur while a wait is in progress
> (i.e. until we are done waiting for the notify), which means I'll be
> waiting a very long time :-).
> What's my confusion?

There are several different locks involved. Each waiting thread
creates a lock, acquires it, and appends it to a list of waiting
locks. It then tries to acquire it *again*, which causes it to wait.
When the notify() method is called, a lock from the waiting list
is released and the thread that was waiting to acquire it a second
time can continue. Note that the thread releasing the lock is
not the one that originally acquired it.

Moreover, there is a single global lock that protects access to the
internal state of the condition variable object. This lock is released
temporarily by a waiting thread and reacquired when it continues.
I suppose that it is this detail that you had overlooked.

> Unfortunately, the documentation is inadequate.  Does anyone know of a
> whole sample program using conditions?

All I can offer is C code that does the equivalent operations
by calling the Python thread library.

> Is the Threading module the right thing to use for multi-threading?

Konrad Hinsen                            | E-Mail: hinsen at
Centre de Biophysique Moleculaire (CNRS) | Tel.: +33-
Rue Charles Sadron                       | Fax:  +33-
45071 Orleans Cedex 2                    | Deutsch/Esperanto/English/
France                                   | Nederlands/Francais

More information about the Python-list mailing list