Python GIL vs CAS
Ned Batchelder
ned at nedbatchelder.com
Thu Feb 15 11:27:34 EST 2018
On 2/15/18 9:35 AM, Chris Angelico wrote:
> On Thu, Feb 15, 2018 at 2:40 PM, Oleg Korsak
> <kamikaze.is.waiting.you at gmail.com> wrote:
>> Hi. While hearing about GIL every time... is there any real reason why CAS
>> doesn't help to solve this problem?
>>
>> https://en.wikipedia.org/wiki/Compare-and-swap
> Because the GIL is not a problem. It's a feature. Before you ask about
> solutions, you need to clarify what you are calling a problem :)
>
Let's not overstate the case. The GIL is not a feature, it's an
effective solution to a problem. The Python interpreter has data that
is shared among threads, including the state of the interpreter, and all
of the reference counts of all of the Python objects. When that data is
modified, it must be done in a thread-safe way. The GIL is a simple and
effective solution to doing that correctly.
Oleg, CAS is a processor primitive that can be used to implement
synchronization tools, including locks like the GIL. You can't replace
the GIL with CAS, since they are not equivalent. You'll need to flesh
out your idea about how CAS can help solve the "mutating shared
interpreter state" problem that the GIL solves.
--Ned.
More information about the Python-list
mailing list