Thread-safety of dict

Duncan Booth duncan.booth at invalid.invalid
Fri Jun 1 05:51:13 EDT 2007


"Adam Olsen" <rhamph at gmail.com> wrote:

> So there you have it: if you're using a dict with custom classes (or
> anything other than str) across multiple threads, and without locking
> it, it's possible (though presumably extremely rare) for a lookup to
> fail even through the key was there the entire time.

Nice work.

It would be an interesting exercise to demonstrate this in practice, and I 
think it should be possible without resorting to threads (by putting 
something to simulate what the other thread would do into the __cmp__ 
method).

I don't understand your reasoning which says it cannot stay in 
ma_smalltable: PyDict_SetItem only calls dictresize when at least 2/3 of 
the slots are filled. You can have 5 items in the small (8 slot) table and 
the dictionary will resize to 32 slots on adding the 6th,the next resize 
comes when you add the 22nd item.



More information about the Python-list mailing list