Simple thread-safe counter?

Paul Rubin http
Sat Apr 2 18:08:38 EST 2005


Tim Peters <tim.peters at gmail.com> writes:
> If you don't want to exploit it, that's fine:  there was always an
> obvious approach using an explicit mutex here, and the only thing
> stopping you from using it is a desire to be clever.  Exploiting the
> GIL in CPython is clever; using an explicit mutex is utterly
> straightforward.  Pick your poison.

For my immediate requirement I'm going to use xrange as you suggested
(thanks!).  It will work exactly as I want.  I'd be uncomfortable
publishing such a program for long-term use by other people, but this
is just for a short-term hack (famous last words).

I think using an explicit mutex goes against Guido's quote mentioned
yesterday, about avoiding tedious code.  But relying on the GIL
results in a brittle program that only works correctly in one Python
implementation.  As use of other implementations becomes more
widespread, one of three things has to happen: 1) Python programs will
become uglier and more mistake-prone (from using explicit mutexes all
over the place); 2) Python programs will become unreliable (from
depending on a GIL that doesn't exist any more); or 3) Python will
acquire some new mechanisms for handling reliable synchronization
easily and cleanly, as other languages already have.

I think (3) is the preferable choice and it's reasonable to try to
figure out how such mechanisms should work.



More information about the Python-list mailing list