locks in threads
Aahz Maruch
aahz at panix.com
Sat Nov 4 13:35:12 EST 2000
In article <slrn9089tk.l4j.bwinton at tor.dhs.org>,
Blake Winton <bwinton at iname.com> 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
More information about the Python-list
mailing list