Critical sections and mutexes

brueckd at brueckd at
Thu Oct 25 15:54:30 CEST 2001

On 25 Oct 2001, David Bolen wrote:

> thread.  Sure there are some pure producer/consumer scenarios that are
> safe, but they're safe in any language, with or without a GIL, to some
> level of granularity.  (E.g., even in C, a producer consumer of a
> shared integer can be considered the same level of safety we're
> talking about here).

Exactly! All languages have a safe level of granularity (certainly not
limited to simple producer/consumer cases); I'm observing that Python has
an expanded level that you can very safely rely on. In a C class that
handles log messages, I wouldn't bother locking access to the integer flag
that tells the current log level, but I certainly would use a lock to
synchronize appending a new log message to the output queue. In Python,
there's no compelling reason to locks in either case. Are there still
cases where I do need locking in Python? Absolutely!

> If you're able to make assumptions in applications and get away with I
> certainly won't stop you from writing that way

Thank goodness! I was worried. :-)

> But the GIL does nothing for application-specific shared resources.
> It only protects the interpreter, so that, as you mentioned in another
> response, a normal Python script can't crash the interpreter.  It does
> nothing to prevent application-specific data structures from becoming
> corrupt and affecting the behavior of the application.

Hey, did you cut and paste that from one of my previous posts? It sounds
an awful lot like what I was saying. :-)

Have fun,

More information about the Python-list mailing list