Threading and Conditions

Konrad Hinsen hinsen at cnrs-orleans.fr
Wed Mar 22 08:38:58 EST 2000


Ken Seehof <kens at sightreader.com> 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?

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



More information about the Python-list mailing list