Threads and racing conditions

Nils Oliver Kröger NO_Kroeger at gmx.de
Wed Aug 22 14:06:28 EDT 2007


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Flavio Preto schrieb:
> Hi,
> 
> I have a doubt. Supose that i have the minimun class below:
> 
> class db:
>     def __init__(self):
>         self.db = {}
>     def read(self, key):
>         return self.db[key]
>     def write(self, key, value):
>         self.db[key] = value
> 
> 
> and an object of this class is shared among some threads. Is it possible
> that in a read, the method return a value that is not an old or a new value?
> In other words, is it possible that a 'read' return (due to a 'write' at the
> same time by another thread) an invalid value that was never supposed to be
> there?
> 
> Thanks,
> Flavio Preto
> 
>

Make the class thread safe by using Lock or RLock ... that will save you
a lot of trouble.
Your limited example might or might not work, but once your class gets
more complex you will most likely run into serious problems.

Greetings

Nils
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFGzHsizvGJy8WEGTcRAgN1AJ42cM1P/NW7Ei3F5ViSsmTcKDvCIgCeIUGG
GD7DAb3f+Lmcav663F+wkQg=
=kJ0G
-----END PGP SIGNATURE-----



More information about the Python-list mailing list