Are min() and max() thread-safe?

Miles Kaufmann milesck at umich.edu
Thu Sep 17 01:08:40 EDT 2009


On Sep 16, 2009, at 9:33 PM, Steven D'Aprano wrote:
> I have two threads, one running min() and the other running max() over
> the same list. I'm getting some mysterious results which I'm having
> trouble debugging. Are min() and max() thread-safe, or am I doing
> something fundamentally silly by having them walk over the same list
> simultaneously?

See for yourself: http://svn.python.org/view/python/trunk/Python/bltinmodule.c?view=markup

min() and max() don't release the GIL, so yes, they are safe, and  
shouldn't see a list in an inconsistent state (with regard to the  
Python interpreter, but not necessarily to your application).  But a  
threaded approach is somewhat silly, since the GIL ensures that they  
*won't* walk over the same list simultaneously (two separate lists,  
for that matter).

-Miles




More information about the Python-list mailing list