SMP, GIL and Threads

Thomas Heller theller at python.net
Fri Dec 16 12:43:13 EST 2005


Steve Holden <steve at holdenweb.com> writes:

> catsup wrote:
>> Hi,
>> I have an app written under version Python 2.3.5.  The problem I'm
>> having is that it hangs on one of its threads.  The thread that hangs
>> does updates to a standard dictionary shared with another thread that
>> only reads this dictionary.  This app works beautifully on a single
>> processor boxes in my testing environment, but this problem quickly
>> occurs when the software runs on a dual cpu SMP blade server with
>> hyperthreading turned off, Windows 2003 server.
>>
> I don't see why you should get problems on SMP hardware, since the
> threads are all part of the same process and should therefore (I'd
> have thought) be tagged with the same processor affinity. Hence the
> GIL should manage contention successfully.
>
>> I do not bother using application level locks because I figure the GIL
>> is going to do the job for me, and because with one thread doing
>> updates and the other only reading, things remain consistent logically
>> for the app.  The app will not have a problem if the dictionary changes
>> just before it does a read.
>>
> I  believe dictionary access is an atomic operation wrt thread
> switches anyway, so I'm (again) not sure why a thread is hanging.

Well, there's at least the 'do not mutate a dictionary (or list) while
iterating over it' advice.  Having one thread writing and another
reading makes it IMO impossible to avoid the conflict.

Thomas




More information about the Python-list mailing list