Is there a way to protect a piece of critical code?
Diez B. Roggisch
deets at nospam.web.de
Wed Jan 10 11:42:00 CET 2007
Hendrik van Rooyen wrote:
> I would like to do the following as one atomic operation:
> 1) Append an item to a list
> 2) Set a Boolean indicator
> It would be almost like getting and holding the GIL,
> to prevent a thread swap out between the two operations.
> - sort of the inverted function than for which the GIL
> seems to be used, which looks like "let go", get control
> back via return from blocking I/O, and then "re - acquire"
> Is this "reversed" usage possible?
> Is there some way to prevent thread swapping?
> The question arises in the context of a multi threaded
> environment where the list is used as a single producer,
> single consumer queue - I can solve my problem in various
> ways, of which this is one, and I am curious as to if it is
> possible to prevent a thread swap from inside the thread.
There have been discussions to make the GIL available from python code. But
it is considered a implementation detail - and this is the reason you can't
do what you need the way you want to.
Just use a regular lock - in the end, that is what the GIL is anyway.
And besides that, you don't "get" anything by your way, as the
thread-scheduling itself isn't controlled by python - instead the OS
threading implementation is used.
More information about the Python-list