FAQ
Hi,

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.

Thanx a lot,

Markus

Search Discussions

  • Aahz Maruch at Nov 3, 2000 at 9:29 pm
    In article <3A02BBE7.E58CD78D at ira.uka.de>,
    Markus von Ehr 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
  • Blake Winton at Nov 4, 2000 at 4:13 pm

    On 3 Nov 2000 13:29:11 -0800, Aahz Maruch wrote:
    In article <3A02BBE7.E58CD78D at ira.uka.de>,
    Markus von Ehr wrote:
    * you need consistant data across multiple variables
    Note that you don't need a lock in this case, as long
    as you store all the data in a Wrapper object, and
    have a method to get a copy of the object, and another
    method to set the object.

    Later,
    Blake.
    --
    9:40pm up 52 days, 21:07, 2 users, load average: 0.02, 0.09, 0.07
  • Aahz Maruch at Nov 4, 2000 at 6:35 pm
    In article <slrn9089tk.l4j.bwinton at tor.dhs.org>,
    Blake Winton wrote:
    On 3 Nov 2000 13:29:11 -0800, Aahz Maruch wrote:

    [when to set a lock]
    * you need consistant data across multiple variables
    Note that you don't need a lock in this case, as long as you store all
    the data in a Wrapper object, and have a method to get a copy of the
    object, and another method to set the object.
    I believe you're wrong. Consider the following trivial example:

    class Foo:
    def set (self, x, y):
    self.x = x
    self.y = y
    def get (self):
    return (x, y)

    If thread 1 calls Foo.set() and thread 2 calls Foo.get(), it is possible
    for the return to be executed after setting self.x but before setting
    self.y. Now, one could in theory do this

    class Foo:
    def set (self, x, y):
    (self.x, self.y) = (x, y)
    def get (self):
    return (x, y)

    but I'm not a fan of relying implicitly on a coding convention in
    multi-threaded programs. Far better to just use a lock in case the
    program changes later.
    --
    --- 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
  • Rodrigo Senra at Nov 5, 2000 at 4:55 am

    Aahz Maruch wrote:

    I believe you're wrong. Consider the following trivial example:

    class Foo:
    def set (self, x, y):
    self.x = x
    self.y = y
    def get (self):
    return (x, y)
    Just checking, but did you mean return(self.x,self.y) instead ?

    --
    Rodrigo Senra
    Computer Engineer (GPr Sistemas Ltda) rodsenra at correionet.com.br
    MSc Student of Reflection (IC- UNICAMP) Rodrigo.Senra at ic.unicamp.br
    http://www.ic.unicamp.br/~921234 (see also http://www.gpr.com.br)
  • Aahz Maruch at Nov 5, 2000 at 3:39 pm
    In article <mailman.973400357.13037.python-list at python.org>,
    Rodrigo Senra wrote:
    Aahz Maruch wrote:
    I believe you're wrong. Consider the following trivial example:

    class Foo:
    def set (self, x, y):
    self.x = x
    self.y = y
    def get (self):
    return (x, y)
    Just checking, but did you mean return(self.x,self.y) instead ?
    Um, yeah. Whoops!
    --
    --- 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

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedNov 3, '00 at 1:21p
activeNov 5, '00 at 3:39p
posts6
users4
websitepython.org

People

Translate

site design / logo © 2022 Grokbase