locks in threads

Aahz Maruch aahz at panix.com
Fri Nov 3 22:29:11 CET 2000


In article <3A02BBE7.E58CD78D at ira.uka.de>,
Markus von Ehr  <vonehr at ira.uka.de> wrote:
>
>I have an one thread running. The user constructs the data (read and
>write) in a complicated class structure and the thread (running every
>100 ms) only has to read this data. Are there any problems (program
>crash etc.) when I don't use variable locks? The thread only has to
>read integers out of the data structure. I don't care if I read an
>older or a newer value.

If the reader thread is only reading one variable (and never updates
that variable), you don't need a lock.  If your thread is reading
multiple variables and you don't need a consistent state between the
variables, you don't need a lock (e.g. for certain kinds of screen
updating).

But if any of the following is true, you need a lock:

* you need consistant data across multiple variables
* more than one thread writes to the variables
* you are updating a complex structure with internal state (this
explicitly includes lists and dictionaries -- there are some exceptions,
but a lock is safer unless you *really* know what you're doing)

Note that you can run into problems with references across threads, so
you need to be careful.
-- 
                      --- Aahz (Copyright 2000 by aahz at pobox.com)

Androgynous poly kinky vanilla queer het    <*>     http://www.rahul.net/aahz/
Hugs and backrubs -- I break Rule 6

"I have the heart of a child.  I keep it in a jar on my desk."  --Robert Bloch



More information about the Python-list mailing list