Critical sections and mutexes

brueckd at tbye.com brueckd at tbye.com
Wed Oct 24 14:59:37 EDT 2001


On Wed, 24 Oct 2001, David Brady wrote:

> > No, "normal" operations on Python objects
> > are atomic as far as threads are concerned.
> [snip]
> > Consider a simple producer/consumer
> > situation:
>
> Yes, but.
>
> Consider this minor rewriting of your code.  Three

[ snipped code rewrite that misses the point ;-) ]

Uh, yeah. That's why in my post I pointed out that there *are* times when
you need some sort of locking/signaling mechanism. In fact, I think I
specifically mentioned something along the lines of enforcing "sequential
access to a function" (i.e. - a mutex or critical section).

I was merely responding to somebody's not-entirely-true assertion that
"_every_ access to that [shared] resource _must_ be enclosed in locks".

Thanks,
Dave

P.S. - Print statements are not atomic and do not fall in the same realm
as "normal" Python operations (e.g. "i = 5") because 'print' involves
a potentially blocking access to an external resource (stdout). To avoid
blocking the interpreter releases its global lock before writing to a file
descriptor and reaquires it when done.





More information about the Python-list mailing list