I have a C extension that is called from my multithreaded Python application. I use a static variable i
somewhere in a C function, and I have a few i++
statements later on that can be run from different Python threads (that variable is only used in my C code though, I don't yield it to Python).
For some reason I haven't met any race condition so far, but I wonder if it's just luck...
I don't have any thread-related C code (no Py_BEGIN_ALLOW_THREADS or anything).
I know that the GIL only guarantees single bytecode instructions to be atomic and thread-safe, thus statements as i+=1
in Python are not thread-safe.
But I don't know about a i++
instruction in a C extension. Any help ?
Copyright Notice:Content Author:「DenverCoder9」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/42006337/python-c-api-is-it-thread-safe